Insert section break and header via VBA

D

Dale Fye

Thanks to those of you that have already contributed to this project.

After generating numerous versions Word documents based on data in an Access
database, my client has decided to make a minor change to the Word document.
They would like to see a header on each page that provides a quick reference
to the section of the document we are in. Right now, I am just inserting
this as a line of text at the top of the page at the beginning of a loop
through my recordset.

Now, instead of just starting a new page for each of these records, I think
I need to insert a new section break with page break(except for the first
record), and then create a header for the section. The code currently looks
something like:

set rs = Currentdb.Openrecordset(strSQL, , dbfailonerror)

While not rs.eof
DoEvents

With wdDoc.Application.Selection
.TypeText Text:= "Section label: " & rs("SomeField")
.TypeParagraph

'some other stuff here

rs.MoveNext

If rs.EOF Then
'dont add any more pagebreaks
ElseIf (bDuplex = True) _
And (.Information(wdActiveEndPageNumber) Mod 2 = 1) Then
.InsertBreak Type:=wdPageBreak
.InsertBreak Type:=wdPageBreak
Else
.InsertBreak Type:=wdPageBreak
End If

End With

Wend

Any guidance would be greatly appreciated.
 
J

Jay Freedman

All this machinery is unncessary if the section label appears on the
first page of the section in some unique style (i.e., that style name
isn't used for anything else, although it may have the same formatting
as some other style).

In that case, just insert a StyleRef field in the header -- the same
field for all pages -- that refers to the style you used. The field
will display the text of the most recent occurrence of the style.

See the help topic on StyleRef fields, and also
http://sbarnhill.mvps.org/WordFAQs/StyleRef.htm for more information.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top