2006.09.12 09:39 AM

Programmatic Creation of Multi-Section Word Documents with Different Headers

On an earlier post regarding the deletion of Word document sections, James Hunter wrote to ask for a little help:

I'm having a vexing problem that perhaps you could help with.
I simply want to programatically create a Word document with 3 sections each with a separate heading. When I use the code generated by Word's record macro function, it does not work, messing up the headings to be above the wrong sections.

Surprisingly, I've never done this. I've created new documents, added headers to documents, even unlinked sections with previous sections in order to get them their own headers, but I've never written a routine just to create multiple sections each having their own header.

Not surprisingly, the recorded macro VBA that Mr. Hunter included with his question is just terrible. I know VBA macro recording serves a purpose, and I would guess everyone doing Office automation has at one time or another turned it on to get a clue about some obscure operation, but more often than not the VBA that's generated is miserable. In many cases, especially those involving lots of visual/pane-oriented operations like this one, the code is loaded with tons of extraneous operations and often won't work as expected when run.

Luckily, in this case, the VBA required to do what Mr. Hunter wants is pretty simple. Here's a routine that will create a new document with as many sections as specified in the NumberOfSections parameter with each section sporting a unique header.

Sub CreateMultiSectionDocDiffHeaders(ByVal NumberOfSections As Integer)

  Dim NewDocument     As Document
  Dim SectionCounter  As Integer
  Set NewDocument = Application.Documents.Add()
  NewDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Header #1"
  If NumberOfSections <= 1 Then Exit Sub
  For SectionCounter = 2 To NumberOfSections
    Call NewDocument.Content.InsertBreak(wdSectionBreakNextPage)
  Next SectionCounter

  For SectionCounter = 2 To NumberOfSections
    With NewDocument.Sections(SectionCounter).Headers(wdHeaderFooterPrimary)
      .LinkToPrevious = False
      .Range.Text = "Header #" & CStr(SectionCounter)
    End With
  Next SectionCounter

End Sub

The only thing noteworthy about this routine is that the creation of the sections and subsequent unlinking of section headers from previous section headers are done in separate loops. If you try to do them together in the same loop (i.e., create a section and immediately unlink it), it won't work.

Anyhow, Mr. Hunter, hope that answers your question. If not, just let me know.



TrackBack URL:  https://www.typepad.com/services/trackback/6a00d8341c7bd453ef00d83460d29069e2

Listed below are links to weblogs that reference Programmatic Creation of Multi-Section Word Documents with Different Headers:

» Followup on Enums from Patrick Steele's .NET Blog
In a follow-up to my last post, Eric Bachtal pointed out that I could use a class with only public constants [Read More]

Tracked on Sep 20, 2006 12:00:44 PM