#1
|
|||
|
|||
Find out if Page of Heading 1 has header
Hi,
I am trying to find using VBA weather when I have a HEading 1 - the page has a header. I want to make sure it doesn't. I tried: Code:
sectionNum = .Information(wdActiveEndSectionNumber) Set secTemp = ActiveDocument.Sections(sectionNum) If secTemp.Headers(wdHeaderFooterFirstPage).Exists = True Then 'page has header" End If Is there another field that could show me whether the page has a header? Thanks, Rocky |
#2
|
||||
|
||||
The first page header will exist, even if empty, provided the different first page option is applied to the current section. I suspect what you want is to ensure the first page header has no content. In that case simply deleting the content of the header will achieve the aim of having an empty header e.g. as follows. I have added optional code to show if the header is empty.
Code:
Sub ClearHeader() Dim secTemp As Section Dim sectionNum As Integer Dim oHeader As HeaderFooter sectionNum = Selection.Information(wdActiveEndSectionNumber) Set secTemp = ActiveDocument.Sections(sectionNum) If secTemp.Headers(wdHeaderFooterFirstPage).Exists = True Then Set oHeader = secTemp.Headers(wdHeaderFooterFirstPage) If Len(oHeader.Range) > 1 Then 'Optional oHeader.Range.Delete Else 'optional MsgBox "Header is empty" 'optional End If 'Optional End If End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Thanks!
Thanks. It worked.
|
#4
|
|||
|
|||
One more question - hierarcy for the header text seems deeper
It did work once. But then, it was empty and to get to the header text I had to go to:
oHeader.Range.Sections.First.Headers.Item(1).Range .Text does that make sense? Is there a more "proper" way to get to this info or at least to Item(1)? |
#5
|
||||
|
||||
What EXACTLY are you trying to do? You don't have to empty the range to write to it, you can simply write something else to the range. Set a variable for the particular header you require (there are potentially three header ranges for each section) and write to it. The following shows how all asections and all headers in those sections can be addressed, if they exist:
Code:
Sub HeadersTest() Dim oSection As Section Dim oHeader As HeaderFooter Dim oRng As Range Dim strHeaderType As String For Each oSection In ActiveDocument.Sections For Each oHeader In oSection.Headers If oHeader.Exists Then Select Case oHeader.Index Case 1: strHeaderType = "Primary" Case 2: strHeaderType = "First Page" Case 3: strHeaderType = "Even Pages" End Select Set oRng = oHeader.Range MsgBox "Section " & oSection.Index & vbCr & _ "Header " & strHeaderType & vbCr & _ "Contains: " & oRng.Text oRng.Text = "New header text for Section " & oSection.Index & " Header " & strHeaderType MsgBox "Section " & oSection.Index & " Header " & strHeaderType & " updated to " & vbCr & _ oRng.Text End If Next oHeader Next oSection lbl_Exit: Set oSection = Nothing 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 www.gmayor.com |
#6
|
|||
|
|||
I didn't explain properly what I am trying to do.
I need to search for each Heading 1 in the document and then see that there is no header on the same page. oHeader.Range always seems to be an empty string even if there is a header on the page. In contrast, oHeader.Range.Sections.First.Headers.Item(1).Range .Text always has a value even if there is no header on the page. |
#7
|
||||
|
||||
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 |
#8
|
|||
|
|||
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 |
#9
|
||||
|
||||
The headers are attributes of the section the page is in, not of the page itself.
I would have to see the document to establish what it is that you are seeing and what you need to see, for I have covered all bases with the code sequences already posted.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#10
|
|||
|
|||
How to check if current selection is section break
Hi,
As a temporary workaround for something else, trying to see if the line above my Heading 1 has a section break. When I search in Word for "^b" it finds a section break. However, selection.text has something that looks like an up arrow. Is there a way that I can compare the selection to a section break or wdSectionBreakNextPage? Thanks, Rocky |
#11
|
|||
|
|||
Could it be that the section number is not currect?
I use: sectionNum = .Information(wdActiveEndSectionNumber) Set secTemp = ActiveDocument.Sections(sectionNum) This occurs when the correct heading is selected. However, when I look at the text it has the text of another heading. When I step through the document I see that the section numbers increase. I also tried to see if the line above has a section break. I can't seem to compare to "^b". Could you do that? |
#12
|
||||
|
||||
Rocky2: Please don't ask the same question in multiple threads. I've merged the two threads concerned.
Section numbers returned by .Information(wdActiveEndSectionNumber) will be correct. whether you're Set secTemp = ActiveDocument.Sections(sectionNum) is 'correct' depends on what you're referencing for .Information(wdActiveEndSectionNumber). Since you haven't posted the relevant code, or said what you're expecting, there's know way for anyone here to know one way or the other. Why do you want to check the character before the Heading? Graham has already given you robust code that works for all headers, regardless of whether the preceding character is a Section break. FWIW, Section breaks & page breaks have an ASCII value of 12, whilst column breaks have an ASCII value of 14.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Sorry. It wasn't the same question so I thought it needed to be on a separate thread.
The format of the section was correct. Because there was no section break before some of the header 1s the header was inserted even though the definition of the section was correct. I ended up checking if there was a header on the page and if there was in inserted a section break before the heading 1. Thanks for your help. |
Tags |
headers, heading 1, section breaks |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Can't remove page break between a Heading 1 and a Heading 2 | Nathan8752 | Word | 3 | 08-31-2015 12:41 PM |
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 |
Need to change text in header to next Heading 1 | Kanith | Word | 1 | 06-14-2011 03:35 PM |