Inserting a header in Microsoft Word with VBA
Posted on 2005-04-18
I am creating a report in Word with VBA and I want to change the header when I insert a new section. I want to insert a section break, switch to header view and then send a sequence of TypeText commands to enter a few lines of header text in various formats.
My code successfully inserts the section break (wd is the Word application object):
It then switches to header view and turns off the Same as Previous opiton:
wd.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
wd.Selection.HeaderFooter.LinkToPrevious = Not wd.Selection.HeaderFooter.LinkToPrevious
At this point I want to send a series of commands using:
wd.Selection. + whatever I want to do
wd.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
wd.Selection.Font.Bold = wdToggle
wd.Selection.Font.Name = "Arial"
wd.Selection.Font.Size = 14
wd.Selection.TypeText Text:="Investigations SAR Case Summaries"
but the Selection is not the new empty header that I just inserted with the new section. The Selection is the header from the previous section, so the above code example enters the Bold Arial 14 text "Investigations SAR Case Summaries" at the end of the previous sectio header!
So how do I make the new header (empty) in the new section the Selection object? Thanks.