(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

' Find Appendix A

With ActiveDocument
    .Range.Collapse wdCollapseStart

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

    .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 *