In a way, yes. You use objects. You have access to all the objects required. The Section. The HeaderFooter. Range objects.
For example:
Code:
Dim r As Word.Range
Set r = ActiveDocument.Sections(3). _
Headers(wdHeaderFooterPrimary).Range
r.Text = "Section 3 Header Primary"
Set r = Nothing
Makes a Range object of the Primary headerfooter of Section 3, and makes the text "Section 3 Header Primary".
Done. No View. No Selection Typetext blah blah blah
Say there was text in a header, and you wanted to put something after it.
Code:
Dim r As Word.Range
Set r = ActiveDocument.Sections(2). _
Headers(wdHeaderFooterPrimary).Range
With r
.Collapse Direction:=wdCollapseEnd
.InsertAfter Text:= " more text after"
End With
Set r = Nothing
This makes the Range object r the range of Section 2 Primary header, collapses the range to the end, and then inserts text " more text after".
Say you had a document with six pages, like this:
Section 1 - pages 1 to 3
Section 2 - pages 3 to 6
Section 1 had DifferentFirstPage, but it is a title page so the footer text is blank. Pages 2 and 3 have, oh, a Table of Contents and their footer has the text "Table of Contents"
Section 2 does NOT have DifferentFirstPage, it is just Primary for the whole Section.
Code:
Dim r As Word.Range
Dim oHF As HeaderFooter
Dim oSection As Section
Set oSection = ActiveDocument.Sections(2)
If oSection.PageSetup. _
DifferentFirstPageHeaderFooter = False Then
oSection.PageSetup.DifferentFirstPageHeaderFooter _
= True
End If
Set oHF = oSection.Footers(wdHeaderFooterFirstPage)
With oHF
.LinkToPrevious = False
.Range.Text = "Footer text - Section 2 First Page" _
& vbTab
Set r = oHF.Range
r.Collapse Direction:=wdCollapseEnd
.Range.Fields.Add Range:=r, Type:=wdFieldFileName
End With
Set r = Nothing
Set oHF = Nothing
This:
1. makes a Section object of Section 2
2. checks to see if it is DifferentFirstPage
3. if it is not, makes it DifferentFirstPage
4. makes a HeaderFooter object of the DifferentFirstPage footer
5. unlinks the HeaderFooter object to Previous, remember the previous is ""
6. adds text "Footer text - Section 2 First Page", and Tab
7. makes a Range object of the footer (ie. the text)
8. collapses the Range to the end
9. adds the Field for the filename
The point being is that you can pretty do anything you want with headers and footers
directly. You do not have to use View, or Selection to type in text. It can all be done behind the scenes, as it were.
It really helps to understand how Word deals with headers and footers.
ALWAYS remember that every Section has three headers (whether you have put anything in them, or not), and three footers (whether you put anything in them, or not).
ALWAYS remember that any time you make a new Section, those headers and footers
WILL be set as Same as previous. If you do not want them repeated from the previous Section you must turn off Same as previous (or in code .LinkToPrevious = False).
What I do is have three procedures which I added to the Insert menu.
SectionNewSame - makes a new Section WITH same as previous
SectionNewEdit - makes a new Section WITH same as previous structure, but displays a userform to make possible partial (or even full) changes. Current settings and values are displayed on the form, and I can make any changes to all SIX headers and footer settings and text from one form. This makes it easy to say, keep a DifferentFirstPage footer text, but change a DifferentFirstPage header text.
SectionNewClear - makes a new Section clearing all Same as previous settings, AND text, ie. all headers and footers are blank.
Just for comparison, here is the code that results from recording a macro to get the EXACT SAME results as my code above.
Code:
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1.25)
.RightMargin = InchesToPoints(1.25)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = True
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
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
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.TypeText Text:="Footer text - Section 2 First Page " & vbTab
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Bleeeech. Other than the fact that using Selection uses system resources (as it uses the user interface), recorded macros often have all this extraneous stuff. Recorded macros ALWAYS use Selection. They can not create objects.
Gerry
My paintings and sculpture