Inserting a header in Microsoft Word with VBA

Posted on 2005-04-18
Medium Priority
Last Modified: 2010-07-27
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):

 wd.Selection.InsertBreak TYPE:=wdSectionBreakNextPage

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

for example,

 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.

Question by:paulmcneil
  • 2
LVL 76

Expert Comment

ID: 13806247
This command will move the Selection to the header in the next section

Selection.Document.Sections(Selection.Sections(1).Index + 1).Headers(wdHeaderFooterPrimary).Range.select

However, I question whether it is a good idea to use the selection object at all. If you have to start with somewhere that the User selects, then it might be unavoidable, but I prefer to work on the underlying document.

Also do you mean just to toggle the Link to previous, or to set it to false?
LVL 18

Expert Comment

ID: 13806260
Selection.Delete 1, 10 'where 10 is the length of the previous header section you want deleted

Put this before the typetext line

LVL 76

Accepted Solution

GrahamSkan earned 2000 total points
ID: 13806481
I mean something like this. This inserts a new section at the end of the document, and works on the header of the new last section.

Dim rng As Word.Range '(or Object if you are using late binding)
Dim Doc As Word.Document
Dim hdr As HeaderFooter
Dim wd As New Word.Application

Set Doc = wd.Documents.Open("C:\mypath\mydoc.doc")
Set rng = Doc.Range
rng.Collapse wdCollapseEnd
rng.InsertBreak wdSectionBreakNextPage
Set hdr = Doc.Sections(Doc.Sections.Count).Headers(wdHeaderFooterPrimary)
hdr.LinkToPrevious = False
hdr.Range.Text = "Investigations SAR Case Summaries"
hdr.Range.Font.Bold = True
hdr.Range.Font.Name = "Arial"
hdr.Range.Font.size = 14
hdr.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

Author Comment

ID: 13809143
GrahamSkan, your sample code was very helpful. I was able to incorporate header and range objects to control what I wanted to do with existing header text. Thanks

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question