Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

Thread Tools Rate Thread Display Modes
Old 05-14-2015, 02:01 PM   #1
Newly Registered User
Join Date: May 2015
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
besuchanko is on a distinguished road
Add different header in each section using automation to Word (VBA)

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

x = x + 1

besuchanko is offline   Reply With Quote
Old 05-16-2015, 11:13 AM   #2
Join Date: Jan 2010
Location: U.K.
Posts: 26,374
Thanks: 0
Thanked 2,423 Times in 2,389 Posts
vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all vbaInet is a name known to all
Re: Add different header in each section using automation to Word (VBA)


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/...splay.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:
    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
    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.
vbaInet is offline   Reply With Quote

automation , header , section , vba , word

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can not see all the controls in the header section Kamakinto Forms 11 06-09-2012 08:36 AM
Header section as Title 4 every page Diana Avila Forms 3 07-15-2011 08:05 AM
word automation - sending multiple records to a table in word mazza Modules & VBA 0 08-03-2007 07:04 AM
Hidden Header section on form... help please! m3ckon Reports 3 12-08-2005 10:16 AM
Values in Header Section of Chart Nancy Reports 2 08-01-2000 01:31 PM

All times are GMT -8. The time now is 10:23 PM.

Microsoft Access Help
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post

Sponsored Links

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World