This macro searches for words through the Word document: Set r = WordDoc.Range. Is it possible to make it search only between specific words in Word document? Example: search only from "Word1" to "Word2". I know that I need to find these words and set them as Range.Start and Range.End, but i'm not good at this. Can someone help me with code?
Code:
Sub test()Dim Word As Object, WordDoc As Object
Dim r As Boolean, f As Boolean, fO As Long
Set Word = CreateObject("Word.Application")
Set WordDoc = Word.Documents.Open(Filename:=Application.ThisWorkbook.path & "\test.docx")
'''name'''
Set r = WordDoc.Range
Do While UnifiedSearch(r, "name*book1")
If f Then
If r.Start = fO Then
Exit Do
End If
Else
fO = r.Start
f = True
End If
WordDoc.Range(r.Start + 4, r.End - 5).Copy
Range("C4").Select
ActiveSheet.Paste
Set r = WordDoc.Range(r.End, r.End)
Loop
WordDoc.Close
Word.Quit
End Sub
Private Function UnifiedSearch(r As Range, s As String) As Boolean
With r.Find
.ClearFormatting
.Text = s
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
UnifiedSearch = .Execute
End With
End Function