Consulting

Results 1 to 3 of 3

Thread: How to search current section header for text?

  1. #1
    VBAX Regular
    Joined
    Jul 2018
    Location
    Blanchard
    Posts
    7
    Location

    How to search current section header for text?

    I would like to know if there is a way to search the text of the current section header in Word. Only want to search one section header. What would that code look like? I am new to VBA programming.

    Thanks in Advance
    Sam Murr

  2. #2
    That's not as straightforward as you might imagine as there are potentially three headers in each section, and those headers may be linked to the preceding or following sections, in which case the header would relate to more than one section. However the following will loop through the headers of the current section, search for the indicated string and if found perform some actions on it.

    Sub Macro1()
    Dim oRng As Range
    Dim oHeader As HeaderFooter
    Dim strFind As String
        strFind = "Text to find"
        For Each oHeader In Selection.Sections(1).Headers
            If oHeader.Exists Then
                Set oRng = oHeader.Range
                With oRng.Find
                    Do While .Execute(FindText:=strFind)
                        'do something with orng (the found text) e.g.
                        oRng.Text = "Replacement Text"
                        oRng.Font.ColorIndex = wdRed
                        oRng.Font.Bold = True
                        oRng.Collapse 0
                    Loop
                End With
            End If
        Next oHeader
    lbl_Exit:
        Set oHeader = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2018
    Location
    Blanchard
    Posts
    7
    Location
    gmayor.....Thanks a bunch! I think this will help me solve my issue! Thanks again!

Tags for this Thread

Posting Permissions

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