Delete Multiple Headers

M

Mickey

Hi,
I have the following code to delete a header. How can I get this to loop
and delete all the headers in a document?

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Thanks in advance for the help. I don't know what I'd do without all your
help.

Mickey
 
S

Stefan Blom

Try this:

Sub DeleteHeaderContents()
Dim s As Section
For Each s In ActiveDocument.Sections
s.Headers(wdHeaderFooterPrimary).Range.Text = ""
s.Headers(wdHeaderFooterEvenPages).Range.Text = ""
s.Headers(wdHeaderFooterFirstPage).Range.Text = ""
Next s
End Sub

--
Stefan Blom
Microsoft Word MVP


in message
news:[email protected]...
 
M

Mickey

Thank you so much. Can you help me with the following code that is adding a
new header which is calling a macro? I need it also to populate each header
in the document. Thanks again. You have saved me hours of time and
frustration!!!

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

Selection.Paragraphs.TabStops.ClearAll
Selection.ParagraphFormat.TabStops.ClearAll
Selection.Paragraphs.TabStops(InchesToPoints(1.25)) _
.Alignment = wdAlignTabLeft
Selection.Paragraphs.TabStops(InchesToPoints(6.5)) _
.Alignment = wdAlignTabRight

Selection.Collapse Direction:=wdCollapseEnd
NormalTemplate.AutoTextEntries("Smokey").Insert _
Where:=Selection.Range, RichText:=True

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
 
S

Stefan Blom

Where do you want the AutoText entry to be inserted? At the second tab stop
in the first paragraph of the main header? If so, the following should work:

Sub AddToMainHeader()
Dim r As Range
Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Paragraphs(1).Range

With r
.ParagraphFormat.TabStops.ClearAll
.ParagraphFormat.TabStops(InchesToPoints(1.25)) _
.Alignment = wdAlignTabLeft
.ParagraphFormat.TabStops(InchesToPoints(6.5)) _
.Alignment = wdAlignTabRight

.Collapse wdCollapseStart
.InsertAfter vbTab
.InsertAfter vbTab
.Collapse wdCollapseEnd
NormalTemplate.AutoTextEntries("Smokey").Insert _
Where:=r, RichText:=True

End With
End Sub

Note that there are other ways to do this... For example, you could add the
tab stops to the Header paragraph (which is applied to all headers by
default).

Note also that you can create an AutoText which has all the contents of the
header (including tab characters, for example); that will make it easier to
insert the contents.

--
Stefan Blom
Microsoft Word MVP


in message
 
S

Stefan Blom

Just loop through all sections, like this:

Sub AddToMainHeader2()
Dim r As Range
Dim s As Section

For Each s In ActiveDocument.Sections
Set r = s.Headers(wdHeaderFooterPrimary) _
.Range.Paragraphs(1).Range

With r
.ParagraphFormat.TabStops.ClearAll
.ParagraphFormat.TabStops(InchesToPoints(1.25)) _
.Alignment = wdAlignTabLeft
.ParagraphFormat.TabStops(InchesToPoints(6.5)) _
.Alignment = wdAlignTabRight

.Collapse wdCollapseStart
.InsertAfter vbTab
.InsertAfter vbTab
.Collapse wdCollapseEnd
NormalTemplate.AutoTextEntries("Smokey").Insert _
Where:=r, RichText:=True

End With
Next s

End Sub

--
Stefan Blom
Microsoft Word MVP


in message
 
M

Mickey

Thank you so much. It works great except it is not carrying over the
bookmarks I have in the autotext header (Smokey). If I run the macro alone
it retains the bookmarks. What I'm doing is running about 4 or 5 macros at
once. The 1st one triggers the 2nd, the 2nd trigger the 3rd, etc. It's then
that I lose the bookmarks. Any suggestions? Thanks again.
 
N

NZ VBA Developer

Mickey,

I don't think it's possible to have more than one bookmark with the same
name in any given document. Therefore, the behaviour you're seeing is as
expected, and I don't believe it has anything to do with the "chaining" of
the macros. When an AutoText entry containing the bookmark is inserted more
than once, the bookmark will be removed from previous instance of the
AutoText entry and "moved" to the most recently inserted instance.

I conducted a "manual" test to confirm this behaviour. I simply inserted an
AutoText entry containing two bookmarks into a blank document and then
confirmed the existence of the bookmarks. I then inserted another instance of
the same AutoText entry into the same document and checked the status of the
bookmarks. As expected there was only one instance of both bookmarks - both
in the last instance of the AutoText entry.

I suggest running your first macro again and seeing if there are multiple
instances of the same bookmark in the document. (I don't think there will
be.) Then I'd have a look at the follow-on macros and see if any of them are
touching bookmarks in any way - or possibly inserting the same or another
AutoText entry containing a bookmark with the same name. I'd also look to see
if there are _any_ instances of bookmarks from the AutoText entry _anywhere_
in the document - both after running the first macro standalone and after
running the macro "chain". If even the one expected instance is being
deleted, then there's something strange going on. However, if there is only
one instance, then Word is behaving exactly as expected.
 
S

Stefan Blom

All I can think of is to link the headers of the various sections; then you
would only have to insert the AutoText once—and any bookmarks in the
AutoText should be left intact (assuming that the bookmarks don't already
exist somewhere else in the document, of course).

--
Stefan Blom
Microsoft Word MVP


in message
 
M

Mickey

Would it be possible to write code that would cross-reference the bookmarks
that were lost to the one that was retained?

Thanks again,
Mickey
 
S

Stefan Blom

Well, my point was that if there is only one header, you wouldn't have to
mess with the bookmarks; they would be preserved, since you would only have
to insert them once (in one of the sections).

--
Stefan Blom
Microsoft Word MVP


in message
 
M

Mickey

Problem is I have multiple headers that can't be linked together. Some are
portrait pages and some are landscape. I know I can cross reference the
unpreserved bookmarks with the preserved ones. Just wondering if there was
any code to do it.

Thanks again for all your help.
 
S

Stefan Blom

OK. I don't know of any such code; but someone else might. :)

--
Stefan Blom
Microsoft Word MVP


in message
 

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