Hi Gil:
This thread seems to have wandered off the track a little
It's neither difficult nor undesirable to do what you want in Word.
Yes, you are quite correct that the Header and footer for a page are
controlled by the section break that most immediately FOLLOWS the page in
the file.
When I say "Controls" I mean that there may be eight section breaks in a
document. If all of their headers and all of their footers are set to "Same
As Previous", the headers and footers will actually be stored in the very
last section break in the file.
The last section break is a special case: it is the default OLE object store
for the file and is hidden below the last paragraph mark in the document.
It contains not only the headers and footers but almost everything else in
the document excepting the text.
When you add section breaks to a document, each one becomes an exact copy of
the one following it. This is a bit counter-intuitive, so commit it to
memory -- it's the key to working with section breaks
Assuming that you break the "Same as previous link", each section break
stores three headers and three footers: First Page, Left Page and Right
Page. The structure of the headers and footers is always there, but each
specific header or footer is not populated until you put something in it.
Once the headers and footers have been populated in a document, you can't
remove them, although you can either delete their content or hide them.
So when working with headers and footers, the first thing you need to do is
set up the default section break in the document. Then each time you insert
a new one, it will have most of the properties you need.
If you're working in VBA, you simply iterate the Stories collection. Here's
some running code that first imports a set of AutoTexts from the template
then adds them to the headers and footers as required, then sets exact
dimensions for them. The headers in this case were expressed as single
vector graphic files that contained a logo. The document itself is set up
for double-sided printing with headers and footers different first/left/ and
right. The routine down the bottom sets a variable in the document so we
can tell what version its headers and footers are.
Hope this helps
Sub ReplaceHeadersFooters()
'
' Macro to put headers and footers right
' Macro written 27 Aug 2004 by John McGhie
'
Dim aVar As Variable
Dim bHeaderDone As Boolean
Dim bStylesDone As Boolean
Dim HeaderVersion As Integer
Dim aSection As Section
Dim aHeader As HeaderFooter
Dim aFooter As HeaderFooter
Dim aStyle As Style
Dim bTrackChanges As Boolean
bTrackChanges = ActiveDocument.TrackRevisions
If ActiveDocument.Type <> wdTypeTemplate Then
Application.OrganizerCopy _
Source:=ActiveDocument.AttachedTemplate.FullName, _
Destination:=ActiveDocument.FullName, _
Name:="Header", _
Object:=wdOrganizerObjectStyles
Application.OrganizerCopy _
Source:=ActiveDocument.AttachedTemplate.FullName, _
Destination:=ActiveDocument.FullName, _
Name:="HeaderLeft", _
Object:=wdOrganizerObjectStyles
Application.OrganizerCopy _
Source:=ActiveDocument.AttachedTemplate.FullName, _
Destination:=ActiveDocument.FullName, _
Name:="FooterRight", _
Object:=wdOrganizerObjectStyles
Application.OrganizerCopy _
Source:=ActiveDocument.AttachedTemplate.FullName, _
Destination:=ActiveDocument.FullName, _
Name:="FooterLeft", _
Object:=wdOrganizerObjectStyles
End If
For Each aSection In ActiveDocument.Sections
With aSection.PageSetup
.SectionStart = wdSectionOddPage
.DifferentFirstPageHeaderFooter = False
.OddAndEvenPagesHeaderFooter = True
.MirrorMargins = True
.Gutter = 0
.TopMargin = InsidePageMargin
.BottomMargin = BottomMargin
.LeftMargin = InsidePageMargin
.RightMargin = OutsidePageMargin
End With
With aSection.Headers(wdHeaderFooterPrimary)
.Range.Delete
ActiveDocument.AttachedTemplate.AutoTextEntries("TenderHeader").Insert
_
Where:=.Range, RichText:=True
.Range.Style = "Header"
Call FormatHeader(.Range)
End With
With aSection.Headers(wdHeaderFooterEvenPages)
.Range.Delete
ActiveDocument.AttachedTemplate.AutoTextEntries("TenderHeader").Insert
_
Where:=.Range, RichText:=True
.Range.Style = "HeaderLeft"
Call FormatHeader(.Range)
End With
With aSection.Footers(wdHeaderFooterPrimary)
.Range.Delete
.Range.Style = "FooterRight"
ActiveDocument.AttachedTemplate.AutoTextEntries("TenderFooterRight").Insert
_
Where:=.Range, RichText:=True
End With
With aSection.Footers(wdHeaderFooterEvenPages)
.Range.Delete
.Range.Style = "FooterLeft"
ActiveDocument.AttachedTemplate.AutoTextEntries("TenderFooterLeft").Insert _
Where:=.Range, RichText:=True
End With
Next ' asection
For Each aVar In ActiveDocument.Variables
If aVar.Name = "HeaderVersion" Then bHeaderDone = True
Next ' aVar
With ActiveDocument.Variables
If bHeaderDone Then
.Item("HeaderVersion").Value = intCurrHeader
Else
.Add Name:="HeaderVersion", Value:=intCurrHeader
End If
End With
With ActiveDocument
.TrackRevisions = bTrackChanges
.ShowRevisions = True
End With
End Sub
Sub FormatHeader(aHeader As Range)
' sets format of picture in header
' macro written 27 Aug 2004 by John McGhie
With aHeader.InlineShapes(1)
.Fill.Visible = msoFalse
.Fill.Transparency = 0#
.Line.Weight = 0#
.Line.Transparency = 0#
.Line.Visible = msoFalse
.LockAspectRatio = msoTrue
.Width = 453.5433
.PictureFormat.Brightness = 0.5
.PictureFormat.Contrast = 0.5
.PictureFormat.ColorType = msoPictureAutomatic
.PictureFormat.CropLeft = 0#
.PictureFormat.CropRight = 0#
.PictureFormat.CropTop = 0#
.PictureFormat.CropBottom = 0#
End With
End Sub
Yet another try, ... guess I don't post all that often.
I hope this formatting works better than what I just posted:
I think I've found a work around for initiating new Header/Footers in an
existing document.
The following code placed at the end or your multipage letter enters an
autotext called "sectHF5" which is comprised of an "empty"
two lines of non-first page header and a non-first page footer with centered
page number. It then substitues some variables
("ptNamx" and "ptDob") and an autotext "myName" from an attached template.
The variables and autotext replace invisible white
colored text ("z.") in each of two lines of header and then converts them to
black.
The user has to be aware not to remove the section break at the bottom of the
file.
One has to make an initial autotext entry for the "blank header" and and save
it as "sectHF5" to normal.dot.
Making a "blank header section break" can be tricky. When making it, you
might need to have a hard page break above the section
break in order to reach "different first pages" in the blank headers.
Why all this? It seems that Word keeps the initial making of a file's H/F in
the last paragraph mark of the entire file. I'm not
sure it ever disappears despite make intervening section breaks. By having
exactly two lines in the header that are blank, one can
code for their removal or replacement. If you try to code deleting 3 header
lines here, you might be deleting a first line of
footer information.
Cheers,
Gil
-------------------
Sub Macro31()
'
' Macro31 Macro
' Macro recorded 1/26/2007 by Gil Carter
'
Application.DisplayAutoCompleteTips = True
NormalTemplate.AutoTextEntries("sectHF5").Insert Where:=Selection.Range, _
RichText:=True
Selection.MoveUp Unit:=wdLine, Count:=3
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="re: "
Application.Run MacroName:="Project.NewMacros.makeNormalFont"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Application.Run MacroName:="Project.makelinemedium.MAIN"
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeText Text:=ptNamx & ", dob " & ptDob & ", date "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DATE \@ ""MMMM d, yyyy"" ", PreserveFormatting:=True
Selection.TypeText Text:=" "
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="by "
Application.Run MacroName:="Project.NewMacros.makeNormalFont"
Application.Run MacroName:="Project.makelinemedium.MAIN"
Selection.TypeBackspace
Selection.TypeBackspace
Application.DisplayAutoCompleteTips = True
Templates.Application.NormalTemplate.AutoTextEntries("myName").Insert
_
Where:=Selection.Range, RichText:=True
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
--
Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.
John McGhie <
[email protected]>
Microsoft MVP, Word and Word for Macintosh. Business Analyst, Consultant
Technical Writer.
Sydney, Australia +61 (0) 4 1209 1410