(Solved) – How to correctly apply .Find in Word documents from VBA Excel?

(solved)-–-how-to-correctly-apply.find-in-word-documents-from-vba-excel?

I am trying to find in a word document (FromDoc) the string Appendix A styled as Heading 1 using the code below. I developed the code in MS Word VBA Editor and worked very well… until I moved it into Excel VBA.

Since I inserted the code into Excel VBA, I have replaced .Selection with ActiveDocument.Range as I discovered the former does not work in Excel VBA. However, now the Execute method fails to find Appendix A. I have spent many hours troubleshooting including reading StackOverflow’s posts, to no avail.

' Activate FromDoc
oApp.Visible = True
oApp.ActiveWindow.Activate
FromDoc.Activate

' Find Appendix A

With ActiveDocument
    .Range.WholeStory
    .Range.Collapse wdCollapseStart
    .Range.Find.ClearFormatting

    With .Range.Find
        .Style = ActiveDocument.Styles("Heading 1")
        .Text = "Appendix A"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
    End With

    .Range.Find.Execute
    .Collapse wdCollapseStart

    Dim r1 As Range
    Set r1 = .Range

End With

I am also suspicious about the Set r1 = .Range. I am concerned it might not work either.

Grateful for your help in understanding and correcting the issue.

Leave a Reply

Your email address will not be published. Required fields are marked *