Detect header/Footer

D

Doug Robbins - Word MVP

Dim Flag As Boolean
Flag = False
With ActiveDocument.Sections(1)
If Len(.Headers(wdHeaderFooterPrimary).Range) > 1 Then Flag = True
If Len(.Headers(wdHeaderFooterFirstPage).Range) > 1 Then Flag = True
If Len(.Footers(wdHeaderFooterPrimary).Range) > 1 Then Flag = True
If Len(.Footers(wdHeaderFooterFirstPage).Range) > 1 Then Flag = True
End With
If Flag = False Then
MsgBox "There is no header or footer."
Else
MsgBox "There is a header of Footer"
End If



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Edward Thrashcort

You could have looked this up in the VBA help, as I did..

with ActiveDocument.Sections(1)
bHeaderActive = .Headers(wdHeaderFooterPrimary).Range.Text = ""
bFooterActive = .Footers(wdHeaderFooterPrimary).Range.Text = ""
end with

You may have to do some research about different types of header and
multiple sections in one document

Eddie
 
J

JethroUK©

i did type in 'header' in vb window and no help was available on this word

also recorded a macro inserting a header and checked out the code, and again
doesn't use the word 'header' or 'footer'

hence i assume that 'headers' & 'footers' are not pre-defined objects
 
G

Greg Maxey

Doug,

Wouldn't your method be inconclusive if the first header or footer was in a
section after section 1?

How about :

Sub ScratchMacro()
Dim oStory As Range
Dim Flag As Boolean
Flag = False
For Each oStory In ActiveDocument.StoryRanges
Do Until (oStory Is Nothing)
Select Case oStory.StoryType
Case Is = 6, 7, 8, 9, 10, 11
If oStory.StoryLength > 1 Then
Flag = True
Exit For
End If
Case Else
'Do Nothing
End Select
Set oStory = oStory.NextStoryRange
Loop
Next
If Flag Then
MsgBox "As header of footer was found."
Else
MsgBox "There is no header or footer."
End If
End Sub
 
D

Doug Robbins - Word MVP

Hi Greg,

Yes, that would certainly be more comprehensive.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jonathan West

JethroUK© said:
i did type in 'header' in vb window and no help was available on this word

also recorded a macro inserting a header and checked out the code, and
again
doesn't use the word 'header' or 'footer'

hence i assume that 'headers' & 'footers' are not pre-defined objects

They are predefined objects - they are collections that are properties of
the Section object.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
T

Tony Jollans

More comprehensive, perhaps, but not a complete check.

Every section always has three header and three footer *objects* which may
or may not have content. Whether or not they are in use is indicated by the
somewhat paradoxical Exists property.

The check on the Range, therefore, should be conditional upon the Exists
property being true.

Sub HFExists()
Dim s As Section
Dim hf As HeaderFooter
Dim Flag As Boolean

For Each s In ActiveDocument.Sections
For Each hf In s.Headers
If hf.Exists Then
If Len(hf.Range.Text) > 1 Then
Flag = True
End If
End If
Next
For Each hf In s.Footers
If hf.Exists Then
If Len(hf.Range.Text) > 1 Then
Flag = True
End If
End If
Next
Next

If Flag Then
MsgBox "A header of footer was found."
Else
MsgBox "There is no header or footer."
End If

End Sub
 
J

Jonathan West

Things get even more complicated by the fact that while a header or footer
might exist, it might not be displayed in a particular section.

The first page header & footer will not display if the
PageSetup.DifferentFirstPageHeaderFooter property of the Section is False

The even pages header will not display if the
PageSetup.OddAndEvenPagesHeaderFooter property of the Section is False.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg

Tony,

While the method you proposed is certainly comprehensive and complete,
I don't see it as more complete than the method I suggested.

The simple fact that a header or footer storylength > 1 confirms that a
header or footer exists and sets the flag to answer the OPs questions.
Once that question is answered there is no need to continue processing.


Actually checking the .Exists condition is rather pointless. Consider
a new blank document and run the following code:

Sub HFExists()
Dim s As Section
Dim hf As HeaderFooter
Dim Flag As Boolean
For Each s In ActiveDocument.Sections
For Each hf In s.Headers
If hf.Exists Then Flag = True
Exit For
Next
Next
If Flag Then
MsgBox "A header exits."
Else
MsgBox "There is no header."
End If
End Sub
 
G

Greg

Jonathan,

It is quite possible that I have just dined on crow and don't quite
follow the OPs intent.
 
T

Tony Jollans

Jonathon,

Aren't you just stating the same as I did in a different way?

However, it made me wonder about another situation - what if there are two
different headers set up in a section, say a first page header with no
content and one for all other pages with some content. Ignoring
complications like continuous section breaks, if the section only has one
page it will have no visible header.
 
T

Tony Jollans

Hi Greg,

Yes, you are right about early termination of the loop - in that sense your
code is better than mine but I think you have missed the point that a header
or footer may have content but be flagged with ".exists = false" and
therefore not play any part in the make up of the complete document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top