Add different header in each section using automation to Word (VBA) (1 Viewer)

besuchanko

New member
Local time
Today, 01:12
Joined
May 14, 2015
Messages
3
I have programmed a letter using automation to Word VBA. The letter works like a mail merge so it might cycle thru several records when it runs. I've separated each letter in the document with a section break. I'm having a problem with the header. I've successfully added a header, but when it moves to the next record, it replaces the header in the entire document with the current record. I want each section to insert data from that record. How can I fix this? Below is a sample of my code (note: the linktoprevious doesn't seem to work either).

x = 1
'Create Header
With ActiveDocument.Sections(x)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Range.Font.Size = 9
.Headers(wdHeaderFooterPrimary).Range.Text = "Session: [" & rst!SessionNum & "] " & rst!SessionTitle & _
Chr(13) & "Presenter: " & rst!Full_Name & Chr(13)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
End With

rst.MoveNext
x = x + 1
 

vbaInet

AWF VIP
Local time
Today, 09:12
Joined
Jan 22, 2010
Messages
26,374
besuchanko,

Your question is best suited in the Word forum but I can see that you're new here so you may not have figured out which forum this should go into. Fyi, this section, 'Modules & VBA' is dedicated to Access. The path to the Word forum is "Access World Forums > Apps and Windows > Word" and the link is http://www.access-programmers.co.uk/forums/forumdisplay.php?f=54

In any case, I'm not a Word VBA guy but this should be simple enough. Here's a revised code which does what you require, please note the comments:
Code:
    x = 1
    
    ' Alter header and create new page
    With ActiveDocument.Sections(x).Headers(wdHeaderFooterPrimary)
        .LinkToPrevious = False
        .Range.Font.Size = 9
        .Range.Text = "Session: [" & rst!SessionNum & "] " & rst!SessionTitle & _
                      Chr(13) & "Presenter: " & rst!Full_Name & Chr(13)
    End With
    
    ActiveDocument.Sections.Last.Range.InsertBreak Type:=wdSectionBreakNextPage  ' inserts a new page section
    rst.MoveNext
    x = x + 1
By the way, if this is going into a loop, I don't see the need of the x = x+1 bit.
 

Users who are viewing this thread

Top Bottom