VBA编译错误,在Word文档上执行.Find方法时参数不是可选的

问题描述

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

我正在Word文档中搜索一个姓(100%确认存在),但是宏是通过Excel VBA编辑器编写的,并在其中进行午餐。

我正在观看此video,并将代码重写到我的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"

它看起来像这样:

enter image description here

您对如何使其一开始有任何想法吗?

为什么这个.find属性不适合在那里? 我找到了这个site,它说.find是Selection的属性,在我的宏中,它被用作Range。找到一个财产,但是。 Find在Range上完美运行,不是吗?

解决方法

查找下面的工作代码主要是由于@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