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.

Re: Remove all contents from the Header of a document by macropod

macropod
Thu Oct 27 02:40:45 CDT 2005

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


"Casey Mac" <info@srvtools.com> wrote in message
news:uM5m5Nr2FHA.1980@TK2MSFTNGP15.phx.gbl...
> 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.
>
>



Re: Remove all contents from the Header of a document by Charles

Charles
Thu Oct 27 07:47:56 CDT 2005

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

See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome!
--------- --------- --------- --------- --------- ---------
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.

"Casey Mac" <info@srvtools.com> wrote in message
news:uM5m5Nr2FHA.1980@TK2MSFTNGP15.phx.gbl...
>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.
>



Re: Remove all contents from the Header of a document by Casey

Casey
Thu Oct 27 20:52:11 CDT 2005

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



Re: Remove all contents from the Header of a document by Casey

Casey
Fri Oct 28 00:37:06 CDT 2005

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



Re: Remove all contents from the Header of a document by macropod

macropod
Fri Oct 28 03:41:45 CDT 2005

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


"Casey Mac" <info@srvtools.com> wrote in message
news:ukPlyH22FHA.2472@TK2MSFTNGP12.phx.gbl...
> 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
>
>



Re: Remove all contents from the Header of a document by Casey

Casey
Fri Oct 28 10:00:40 CDT 2005

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



Re: Remove all contents from the Header of a document by Greg

Greg
Fri Oct 28 10:42:53 CDT 2005

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


Re: Remove all contents from the Header of a document by Casey

Casey
Sun Oct 30 21:24:59 CST 2005

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



Re: Remove all contents from the Header of a document by Greg

Greg
Sun Oct 30 22:03:06 CST 2005

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


Re: Remove all contents from the Header of a document by Casey

Casey
Sun Oct 30 23:41:31 CST 2005

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



Re: Remove all contents from the Header of a document by Greg

Greg
Mon Oct 31 06:48:02 CST 2005

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.


Re: Remove all contents from the Header of a document by Casey

Casey
Mon Oct 31 17:16:45 CST 2005

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.



Re: Remove all contents from the Header of a document by Greg

Greg
Mon Oct 31 17:49:08 CST 2005

Casey Mac,

No, sorry I haven't a clue.

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

Casey Mac wrote:
> 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.



Re: Remove all contents from the Header of a document by Casey

Casey
Mon Oct 31 18:22:28 CST 2005

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.



Re: Remove all contents from the Header of a document by Greg

Greg
Mon Oct 31 19:48:08 CST 2005

Send me the file.