View Single Post
 
Old 02-11-2016, 05:16 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The macro I posted earlier today will give you a message box for every header in the document, which indicates which header it is, in which section of the document and whatever the text content of that header is. For a new blank document you should see only one message box. For a complex document you might see several.

oHeader.Range is a range not a text string. Of you want the text it contains then you want oHeader.Range.Text, but first you will have to tell the macro what oHeader refers to.

When you say that you want to search for each 'Heading 1' then can we assume that you mean a paragraph with the Heading 1 paragraph style?

The variation below will locate every Heading 1 style, and will report the contents of the headers of the section where that style is found. If there is content in the header you will be given an option to delete it.

You can use the techniques to do whatever you want with the headers in your document.
Code:
Sub Macro1()
Dim oRng As Range
Dim i As Integer
Dim iYes As Integer
Dim strText As String
Dim oHeader As HeaderFooter
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .Style = "Heading 1"
        Do While .Execute
            i = oRng.Information(wdActiveEndSectionNumber)
            For Each oHeader In ActiveDocument.Sections(i).Headers
                If oHeader.Exists Then
                    strText = oHeader.Range.Text
                    If Len(strText) > 1 Then
                        Select Case oHeader.Index
                            Case 1:        'Primary
                                iYes = MsgBox("The primary header content is " & oHeader.Range.Text & vbCr & _
                                              "Delete?", vbYesNo)
                                If iYes = vbYes Then oHeader.Range.Delete
                            Case 2:        'First Page
                                iYes = MsgBox("The first page header content is " & oHeader.Range.Text & vbCr & _
                                              "Delete?", vbYesNo)
                                If iYes = vbYes Then oHeader.Range.Delete
                            Case 3:        'Even Pages0
                                iYes = MsgBox("The even pages header content is " & oHeader.Range.Text & vbCr & _
                                              "Delete?", vbYesNo)
                                If iYes = vbYes Then oHeader.Range.Delete
                        End Select
                    Else
                        Select Case oHeader.Index
                            Case 1:        'Primary
                                MsgBox "The primary header is empty"
                            Case 2:        'First Page
                                MsgBox "The first page header is empty"
                            Case 3:        'Even Pages0
                                MsgBox "The even pages header is empty"
                        End Select
                    End If
                End If
            Next oHeader
            oRng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set oRng = Nothing
    Set oHeader = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote