Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Detect header/Footer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JethroUK© - 27 Nov 2005 17:16 GMT
Is it possible to detect whether a header/footer is present in the activedoc
Doug Robbins - Word MVP - 27 Nov 2005 17:48 GMT
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

Signature

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

> Is it possible to detect whether a header/footer is present in the
> activedoc
Greg Maxey - 27 Nov 2005 19:02 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Dim Flag As Boolean
> Flag = False
[quoted text clipped - 12 lines]
>> Is it possible to detect whether a header/footer is present in the
>> activedoc
Doug Robbins - Word MVP - 27 Nov 2005 21:21 GMT
Hi Greg,

Yes, that would certainly be more comprehensive.

Signature

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

> Doug,
>
[quoted text clipped - 44 lines]
>>> Is it possible to detect whether a header/footer is present in the
>>> activedoc
Tony Jollans - 28 Nov 2005 14:24 GMT
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

--
Enjoy,
Tony

> Hi Greg,
>
[quoted text clipped - 62 lines]
> >>> Is it possible to detect whether a header/footer is present in the
> >>> activedoc
Jonathan West - 28 Nov 2005 14:33 GMT
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.

Signature

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

> More comprehensive, perhaps, but not a complete check.
>
[quoted text clipped - 107 lines]
>> >>> Is it possible to detect whether a header/footer is present in the
>> >>> activedoc
Greg - 28 Nov 2005 15:17 GMT
Jonathan,

It is quite possible that I have just dined on crow and don't quite
follow the OPs intent.
Tony Jollans - 28 Nov 2005 17:04 GMT
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.

--
Enjoy,
Tony

> 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.
[quoted text clipped - 123 lines]
> >> >>> Is it possible to detect whether a header/footer is present in the
> >> >>> activedoc
Greg - 28 Nov 2005 15:07 GMT
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
Tony Jollans - 28 Nov 2005 17:13 GMT
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.

--
Enjoy,
Tony

> Tony,
>
[quoted text clipped - 24 lines]
> End If
> End Sub
Edward Thrashcort - 27 Nov 2005 18:00 GMT
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

> Is it possible to detect whether a header/footer is present in the
> activedoc
JethroUK© - 27 Nov 2005 18:22 GMT
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

> You could have looked this up in the VBA help, as I did..
>
[quoted text clipped - 10 lines]
> > Is it possible to detect whether a header/footer is present in the
> > activedoc
Jonathan West - 28 Nov 2005 12:22 GMT
>i did type in 'header' in vb window and no help was available on this word
>
[quoted text clipped - 3 lines]
>
> 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.

Signature

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 

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.