Stackoverflow community. I'm in the process of learning. Find method in VBA.
I'm searching for a surname (100% sure it's there) in a Word document, but the macro is written and lunched from the Excel VBA editor.
I'm watching this video and rewritten the code into my VBA editor, I've checked it and I'm running it.
I've pasted the code below:
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
Before macro even starts I get "Compile error, Argument not optional" in this line:
'Define the Find Object based on the range
Set wrdFind = wrdRng.Find 'this line gives the "Compile error, Argument not optional"
It looks like this:
Do you have any Ideas on how to make it work in the first place?
And why this.find property doesn't fit there? I've found this site where it says that.find is Selection's property and in my macro, it's used as a Range. Find a property, but. Find works flawlessly with Range, doesn't it??
Finding the working code below was mostly thanks to @GSreg comments and @Zac direction.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.