首页 > 解决方案 > VBA编译错误,在word文档上执行.Find方法时参数不是可选的

问题描述

Stackoverflow 社区。我正在学习中。在 VBA 中查找方法。

我正在 Word 文档中搜索姓氏(100% 确定它在那里),但宏是从 Excel VBA 编辑器编写和午餐的。

我正在观看此视频并将代码重写到我的 VBA 编辑器中,我已经检查过它并且正在运行它。

我已经粘贴了下面的代码:

Sub UsingTheFindObject_Simple()

'Declare our variables
Dim wrdFind As Find
Dim wrdRng As Range
Dim wrdDoc As Document
Dim srchResult As Boolean

'Grab the active document
Set wrdDoc = GetObject(, "Word.Application.ActiveDocument")
Set excelWrkbook = GetObject(, "Excel.Application.ActiveWorkbook")

'Define the content in this document
Set wrdRng = wrdDoc.Content

'Define the Find Object based on the range
Set wrdFind = wrdRng.Find      'this line gives the "Compile error, Argument not optional"

'Define the parameters of our search
With wrdFind

   'Look for the phrase: TOKAJ-SMOCZKIEWICZ
   .Text = "TOKAJ-SMOCZKIEWICZ"
   .MatchWildcards = False
   .MatchCase = False
   .Forward = True
   
   'Conduct the search if a match it returns TRUE else FALSE
   srchResult = .Execute
   
End With

'If argument is found, display it
If srchResult = True Then
   
   'Display message
   Debug.Print "Found the word" & wrdRng.Find & ", now formatting."
   
   'Change the font to bold
   wrdRng.Bold = True
   
End If

End Sub

在宏开始之前,我在这一行中得到“编译错误,参数不是可选的”:

'Define the Find Object based on the range
Set wrdFind = wrdRng.Find        'this line gives the "Compile error, Argument not optional"

它看起来像这样:

在此处输入图像描述

您对如何使其首先发挥作用有任何想法吗?

为什么这个 .find 属性不适合那里?我发现这个网站说 .find 是选择的属性,在我的宏中,它被用作范围。找到一个属性,但是。Find 与 Range 完美配合,不是吗?

标签: excelvbams-word

解决方案


找到下面的工作代码主要归功于@GSreg 注释和@Zac 指导。

Sub UsingTheFindObject_Simple()

'Declare our variables
Dim wrdApp As Word.Application
Dim exclApp As Excel.Application

Dim wrdFind As Find
Dim wrdRng As Word.Range
Dim wrdDoc As Word.Document
Dim mySheet As Excel.Worksheet
Dim exclWrkbook As Excel.Workbook
Dim srchResult As Boolean

'Grab the active document
Set wrdApp = GetObject(, "Word.Application")       'At its simplest, CreateObject creates an instance of an object,
Set exclApp = GetObject(, "Excel.Application")     'whereas GetObject gets an existing instance of an object.

Set wrdDoc = wrdApp.ActiveDocument
'Set wrdDoc = GetObject(, "Word.Application.ActiveDocument")           'This line alone doesn't work.
Set exclWrkbook = exclApp.ActiveWorkbook
'Set exclWrkbook = GetObject(, "Excel.Application.ActiveWorkbook")     'This line alone doesn't work.
Set mySheet = Application.ActiveWorkbook.ActiveSheet

'Define the content in this document
Set wrdRng = wrdDoc.Content

'Define the Find Object based on the range
Set wrdFind = wrdRng.Find

'Define the parameters of our search
With wrdFind

   'Look for the phrase: TOKAJ-SMOCZKIEWICZ
   .Text = "TOKAJ-SMOCZKIEWICZ"
   .MatchWildcards = False
   .MatchCase = False
   .Forward = True
   
   'Conduct the search if a match it returns TRUE else FALSE
   srchResult = .Execute
   'Debug.Print wrdFind      'Object doesn't support this property or method.
    Debug.Print wrdRng
End With

'If argument is found, display it
If srchResult = True Then
   
   'Display message
   Debug.Print "Found the word " & wrdRng & ", now formatting."
   
   'Change the font to bold
   wrdRng.Bold = True
   
End If

End Sub

推荐阅读