none
vba search word document headings (e.g. h1 heading) only RRS feed

  • Question

  • Hi guys & gals! Does anyone know if it is possible to search a word document - but only in the headings section (e.g. search for a word match but only in the h1 headings).
    I have no idea of how to go about this, have only done some basic macros to e.g. set the size of the sidebar and collapse the outline view. Any suggestions greatly appreciated.

    Sam.
    Saturday, May 7, 2016 1:57 PM

All replies

  • Maybe something like

    Sub Macro1()
    Dim oRng As Range
    Dim strFind As String: strFind = "Lorem"
    Dim strStyle As String: strStyle = "Heading 1"
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Style = strStyle
            Do While .Execute(FindText:=strFind)
                'do something with the found range (orng)' e.g.
                oRng.HighlightColorIndex = wdYellow
                oRng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    

    or

    Sub Macro2()
    Dim oRng As Range
    Dim strFind As String: strFind = "Lorem"
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:=strFind)
                If oRng.Paragraphs(1).Range.ListParagraphs.Count = 1 Then
                    If oRng.Paragraphs(1).Range.ListFormat.ListLevelNumber = 1 Then
                        'do something with the found range (orng) e.g.'
                        oRng.HighlightColorIndex = wdYellow
                    End If
                End If
                oRng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    
    


    Graham Mayor - Word MVP
    www.gmayor.com

    Sunday, May 8, 2016 5:58 AM