Results 1 to 6 of 6

Thread: VBA - find text in a word doc headers and return all page numbers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Here's one way:
    Sub Demo()
    Dim Rct As Rectangle, i As Long, StrOut As String
    With ActiveDocument
      For i = 1 To .ComputeStatistics(wdStatisticPages)
        With .ActiveWindow.Panes(1).Pages(i)
          For Each Rct In .Rectangles
            With Rct.Range.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Text = "Text to find"
              .Forward = True
              .Wrap = wdFindStop
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
              .Execute
              If .Found = True Then
                 StrOut = StrOut & vbCr & i: Exit for
              End If
            End With
          Next
        End With
      Next
    End With
    MsgBox "Text found on pages:" & StrOut
    End Sub
    The above code will potentially test every page in full, so it could be somewhat slower than a standard looped Find.
    Last edited by macropod; 04-05-2021 at 07:43 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •