Remove all contents from the Header of a document

C

Casey Mac

I have some documents that are being inserted into an exhisting document.
Some of the documents have headers that are changing my template. Is there
a macro to get rid of all contents in a header. Including any tables that
might be in there? I mean anything that is in there.

Please advise.

thanks in advance.
 
M

macropod

Hi Casey,

No need to cross-post. One post in an appropriate forum will almost always
do!

Here's some code to delete the header content in the active document. You'll
either need to switch to the document to be inserted, or change
'ActiveDocument' to refer to it explicitly, before executing the sub -
otherwise you'll end up deleting the headers from the wrong document ...

Sub KillHeaders()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
Next
Next
End Sub

Cheers
 
C

Charles Kenyon

What you really need is to be more careful in your selection of copied
material. See
http://word.mvps.org/FAQs/Formatting/PasteWithoutSectionInfo.htm.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Casey Mac

My bad about the over posting. Wont happen again. I have one more
question. this code works like a charm but when i then go in to create a
macro to place page numbers at the bottom of the active document...nothing
shows up. Does this code kill any footers/headers for good? My thinking
was to delete any extra crap that i didnt need and then add page numbers.

Help

Sub KillHeaders()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
Next
Next
End Sub
 
C

Casey Mac

Unfortualty the user gets there info from many different sources. Each
unique. I have no control over them. At the end of the day all i care
about is that all there sources get inserted into a single file (from my
template). This is all so that standardization of format can be achieved.
I read the article. It was very infomative. Unfortuantly it doesnt help my
situation.

Here is the process.



The user inserts a file.



Later on the user runs code to get rid of any header or footer information.
(Thanks macropod) ;>



Sub KillHeadersFooters()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
Next
Next

For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
Next
Next


End Sub



Then I try and have the user run this code to reinsert page numbers into
that active document



Sub InsertPageNum()

Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberCenter, FirstPage:=False
End Sub



But nothing happens to the document. No errors are returned either.
Thoughts?



Please help
 
M

macropod

Hi,

The code I posted on works on the headers - anything in the footers is left
alone.

Presumably, you'd be adding the page numbers to the target document, not the
source documents. In that case, simply inserting a page number field in the
footer of the target document should suffice.

Cheers
 
C

Casey Mac

The code you gave worked slick! But i had to alter it later to also do and
footer information. See below. I am trying to get the page number inserted
automatically and even when i do it manually it doesnt work. Please see
below.

Sub KillHeadersFooters()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
Next
Next

For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
Next
Next


End Sub



Then I try and have the user run this code to reinsert page numbers into
that active document



Sub InsertPageNum()

Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberCenter, FirstPage:=False
End Sub
 
G

Greg

Thanks for your promise not to cross posts in the future ;-)

Try this (Note there will be no page number on the first page):

Sub KillHeadersFooters()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
Next
ActiveDocument.Sections(1).Footers(1).PageNumbers.Add
PageNumberAlignment:= _
wdAlignPageNumberCenter, FirstPage:=False
End Sub
 
C

Casey Mac

Greg your code works perfectly but i have one question. I tried altering it
so that the page would a page AutoTextEntries("Page X of Y") but i cant get
it to work. can you help...Please :>


ActiveDocument.Sections(1).Footers(1).PageNumbers.Add
PageNumberAlignment:= _
wdAlignPageNumberCenter, FirstPage:=False
 
G

Greg

Remeber, you promised no more cross posting.

Try:

Sub KillHeadersFooters()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
Dim oRng As Range
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
Next
Set oRng =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=oRng,
RichText:=True
End Sub
 
C

Casey Mac

Sorry about that. I will be more contiencious when i do a reply to all.
The only alteration i would like to try is to have this centered in the
document but my syntax doesnt work. Can i get you to take a look and tell me
what you think. maybe l lost something when i copyied into my code.

Also what does this line mean? RichText:=True

Does that mean that it will only work if the document is Rich Text?


Sub KillHeadersFooters()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
Dim oRng As Range
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
Next
Set oRng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=oRng,
RichText:=True, wdAlignPageNumberCenter:=True, FirstPage:=False

End Sub
 
G

Greg

Try:

Sub KillHeadersFooters()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
Dim oRng As Range
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Delete
On Error Resume Next
oHeadFoot.LinkToPrevious = True
On Error GoTo 0
Next
Next
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
Set oRng =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=oRng,
RichText:=True
End Sub

Look up RichText in VBA help.
 
C

Casey Mac

Thanks for the code. it is trully brilliant!

I have one more question regarding it though. The particular file(s) that
is always problem matick is a Rich text document format. On that location
(specific page of instertion and on the main documents TOC -page 2) the
footer doesnt display at all. Thoughts? Everything else works perfectly.
 
C

Casey Mac

Greg thanks for your efforts. I do have one last question. I ran your code
and got the following:

runtime error '4608"

Value out of Range.

It debugs on this line.

ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True

Also when i added the code i had to change it to the following as parts
turned up red. Code this be my error.

ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
Set oRng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
oRng.ParagraphFormat.Alignment = wdAlignParagraphCenter
NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=oRng,
RichText:=True


Thanks...i really do appreciated it.
 

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