Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-11-2016, 05:16 AM
gmayor's Avatar
gmayor gmayor is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,143
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
  #2  
Old 02-11-2016, 06:40 AM
rocky2 rocky2 is offline Find out if Page of Heading 1 has header Windows 10 Find out if Page of Heading 1 has header Office 2013
Novice
Find out if Page of Heading 1 has header
 
Join Date: Feb 2016
Posts: 25
rocky2 is on a distinguished road
Default

Thank you for your patience.

I have 2 places in the document in which I have a header in a page with a heading that has a style of Heading 1.
In those places I get the same thing:
If Len(strText) > 1 Select Case oHeader.Index
Case 1: 'Primary

and then
else Case oHeader.Index
Case 2: 'First Page
MsgBox "The first page header is empty"

I can't see any difference in the behavior on a page in which I have Heading 1 and a header vs. a page that has a Heading 1 with no header displayed.

Is there another hidden variable I can use?

Thanks,
Rocky
Reply With Quote
Reply

Tags
headers, heading 1, section breaks



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find out if Page of Heading 1 has header Can't remove page break between a Heading 1 and a Heading 2 Nathan8752 Word 3 08-31-2015 12:41 PM
Find out if Page of Heading 1 has header Can I unter the last heading in a footer, regardless of what level the header is? Huffle Word 2 09-28-2014 03:48 PM
include heading text in header eNGiNe Word 2 03-06-2013 12:24 AM
How to have a heading 1 file automatically appear in each header of any page? expert4knowledge Word 2 09-16-2012 10:38 AM
Find out if Page of Heading 1 has header Need to change text in header to next Heading 1 Kanith Word 1 06-14-2011 03:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:53 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft