![]() |
|
#1
|
||||
|
||||
![]()
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 |
#2
|
|||
|
|||
![]()
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 |
![]() |
Tags |
headers, heading 1, section breaks |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Nathan8752 | Word | 3 | 08-31-2015 12:41 PM |
![]() |
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 |
![]() |
Kanith | Word | 1 | 06-14-2011 03:35 PM |