Inserting a header in Microsoft Word with VBA

Posted on 2005-04-18
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
    LVL 76

    Expert Comment

    This command will move the Selection to the header in the next section

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

    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

    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

    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

    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now