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 2007

Tip: Looking for answers? Try searching our database.

Delete Multiple Headers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mickey - 30 Oct 2007 04:17 GMT
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
Stefan Blom - 30 Oct 2007 11:37 GMT
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

Signature

Stefan Blom
Microsoft Word MVP

> Hi,
> I have the following code to delete a header.  How can I get this to loop
[quoted text clipped - 9 lines]
>
> Mickey
Mickey - 30 Oct 2007 13:44 GMT
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

> Try this:
>
[quoted text clipped - 20 lines]
> >
> > Mickey
Stefan Blom - 30 Oct 2007 14:48 GMT
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.

Signature

Stefan Blom
Microsoft Word MVP

> Thank you so much.  Can you help me with the following code that is adding
> a
[quoted text clipped - 44 lines]
>> >
>> > Mickey
Mickey - 30 Oct 2007 15:18 GMT
It works but it's not adding the header to all the sections.  Thanks

> 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:
[quoted text clipped - 77 lines]
> >> >
> >> > Mickey
Stefan Blom - 30 Oct 2007 15:25 GMT
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

Signature

Stefan Blom
Microsoft Word MVP

> It works but it's not adding the header to all the sections.  Thanks
>
[quoted text clipped - 86 lines]
>> >> >
>> >> > Mickey
Mickey - 30 Oct 2007 16:23 GMT
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.

> Just loop through all sections, like this:
>
[quoted text clipped - 115 lines]
> >> >> >
> >> >> > Mickey
NZ VBA Developer - 30 Oct 2007 21:35 GMT
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.
Signature

Cheers!
The Kiwi Koder

> 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
[quoted text clipped - 121 lines]
> > >> >> >
> > >> >> > Mickey
Stefan Blom - 31 Oct 2007 12:45 GMT
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).

Signature

Stefan Blom
Microsoft Word MVP

> 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
[quoted text clipped - 129 lines]
>> >> >> >
>> >> >> > Mickey
Mickey - 01 Nov 2007 01:32 GMT
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

> 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
[quoted text clipped - 134 lines]
> >> >> >> >
> >> >> >> > Mickey
Stefan Blom - 01 Nov 2007 15:11 GMT
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).

Signature

Stefan Blom
Microsoft Word MVP

> Would it be possible to write code that would cross-reference the
> bookmarks
[quoted text clipped - 153 lines]
>> >> >> >> >
>> >> >> >> > Mickey
Mickey - 01 Nov 2007 15:51 GMT
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.

> 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
[quoted text clipped - 157 lines]
> >> >> >> >> >
> >> >> >> >> > Mickey
Stefan Blom - 01 Nov 2007 16:02 GMT
OK. I don't know of any such code; but someone else might.  :-)

Signature

Stefan Blom
Microsoft Word MVP

> Problem is I have multiple headers that can't be linked together.  Some
> are
[quoted text clipped - 182 lines]
>> >> >> >> >> >
>> >> >> >> >> > Mickey
 
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.