Insert Footer Page x of y via Access to Word Template

G

Guest

A user will enter/select data from an Access form. Then
VBA takes the users responses and inserts them into the
Word document. So far that is working fine.

My problem lies with the footers. There are 3 sections to
this document. I have been able to set the footers for
sections 1 and 2 via VBA code.

Section 3 needs to read something like this:

left side - INFORMATION PULLED FROM ACCESS FORM
center - Page x of y (total pages within section 3)
right side - current date

How do I pull the total number of pages within the section?

Thank you in advance.

Cindy
 
J

Jay Freedman

A user will enter/select data from an Access form. Then
VBA takes the users responses and inserts them into the
Word document. So far that is working fine.

My problem lies with the footers. There are 3 sections to
this document. I have been able to set the footers for
sections 1 and 2 via VBA code.

Section 3 needs to read something like this:

left side - INFORMATION PULLED FROM ACCESS FORM
center - Page x of y (total pages within section 3)
right side - current date

How do I pull the total number of pages within the section?

Thank you in advance.

Cindy

Use a SectionPages field.
 
C

Cindy

This is the first time I have used Access to open a Word
document and insert data from Access into Word and my
first experience at using collections. With that, my
question may sound silly, but how do I access the
SectionPages property?

In the Access VBA (located behind the form), I Dim and Set
the Word document
Dim doc as word.document
set doc = .documents(template_name)
{perform error check that it found template}
set doc = .documents.Open(path & template_name,,true)
Once my document is open, I load the .formfields object.
..formfields("fldWordBookmarkName").result = variable or
object on Access form

After the .formfields have been loaded, I proceed to load
the footers.

Dim the Section object
Dim oSec as Word.Section
Set the Section object (done within the .doc object)
Set oSec = .Sections(3)
Load text within the footer
oSec.Footers(wdHeaderFooterPrimary).Range.Text = "text"
Load page number within the footer
oSec.FooterswdHeaderFooterPrimary).PageNumbers.Add.Alignmen
t = wdAlignPageNumberCenter

Within the PageNumbers and the HeadersFooters collection
object, I have not found the "SectionPages". Where can I
find this?

Thanks.

Cindy
 
J

Jay Freedman

Hi Cindy,

First, you need to know that SectionPages is a *field*, not a *property* of
anything in VBA. Word uses fields to insert all sorts of calculated values
into documents. Please read at least Word's (regular, not VBA) help topic
about fields and field codes so you know what you're dealing with.

Second, your code that opens the document gives me fits. In Word, "template"
has a special meaning. A template is not an ordinary document that you open
and fill in; it's a separate file that's used like a cookie-cutter to make
other documents. Once a template is created for a specific purpose, it's
rarely opened or altered -- you do all the work in documents *based on* the
template. (See
http://word.mvps.org/FAQs/Customization/CreateATemplatePart1.htm.) In VBA,
you would use

Set doc = .Documents.Add(template_name)

where the template called template_name already exists, and that command
creates a new document *based on* the template. Use .Documents.Open only for
regular documents, unless you mean to alter the template itself.

My best suggestion is that you should create a Word template specifically
for this kind of document, in which the footer is already set up with the
Page field, the SectionPages field, and the CreateDate field in place. The
footer of the template can also contain a bookmark, which would then be
present in the document, and the Access code can insert its text at the
bookmark in the document (see
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm).

Another consideration is that you have to unlink the footer of Section 3
from the footer of Section 2, or else any changes you make will appear in
both footers. You can do that in the template by going into the footer area
of Section 3 and clicking the Same As Previous button on the Footer toolbar
to turn it off, and then the document will inherit this from the template.
You would also want to restart page numbering at page 1 in Section 3
(otherwise your footer will say nonsense like "Page 3 of 1").

In Word, by default the footers are formatted with a paragraph style named
Footer, which contains a center-aligned tabstop at the middle of the text
width and a right-aligned tabstop at the right margin. Use tab characters
(vbTab) to separate the pieces of your footer, and they'll automatically
align correctly. Also, you can't use ".PageNumbers.Add" (equivalent to the
Insert > Page Number dialog) to put in a page number that will be part of
the "Page x of y" construction -- that kind of page number is stuck into a
frame that floats over the footer text.

If you really want to do this footer stuff the hard way through VBA code,
here's an example of what you need to do to insert the text and fields in
the right places (I assume your document already has at least three
sections).

Dim oRg As Word.Range

With ActiveDocument ' in Access, use "With doc"
Set oSec = .Sections(3)

With oSec.Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False ' turn off Same As Previous
.PageNumbers.RestartNumberingAtSection = True
.PageNumbers.StartingNumber = 1
End With

Set oRg = oSec.Footers(wdHeaderFooterPrimary).Range

' left side (text actually from Access DB)
' and start of center
oRg.Text = "text" & vbTab & "Page "
Set oRg = oSec.Footers(wdHeaderFooterPrimary).Range
oRg.Collapse direction:=wdCollapseEnd

' Page field
.Fields.Add Range:=oRg, Type:=wdFieldPage
Set oRg = oSec.Footers(wdHeaderFooterPrimary).Range
oRg.Collapse direction:=wdCollapseEnd

oRg.Text = " of "
Set oRg = oSec.Footers(wdHeaderFooterPrimary).Range
oRg.Collapse direction:=wdCollapseEnd

' SectionPages field
.Fields.Add Range:=oRg, Type:=wdFieldSectionPages
Set oRg = oSec.Footers(wdHeaderFooterPrimary).Range
oRg.Collapse direction:=wdCollapseEnd

oRg.Text = vbTab
Set oRg = oSec.Footers(wdHeaderFooterPrimary).Range
oRg.Collapse direction:=wdCollapseEnd

' CreateDate field
.Fields.Add Range:=oRg, Type:=wdFieldCreateDate, _
Text:="\@MM/dd/yyyy"
End With

Set oRg = oSec.Footers(wdHeaderFooterPrimary).Range
oRg.Fields.Update
 

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