Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

Insert Footer Page x of y via  Access to Word Template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
anonymous@discussions.microsoft.com - 09 Mar 2005 22:51 GMT
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
Jay Freedman - 10 Mar 2005 02:08 GMT
>A user will enter/select data from an Access form. Then
>VBA takes the users responses and inserts them into the
[quoted text clipped - 15 lines]
>
>Cindy

Use a SectionPages field.

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
Cindy - 10 Mar 2005 16:30 GMT
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

>-----Original Message-----
>
[quoted text clipped - 25 lines]
>Microsoft Word MVP         FAQ: http://word.mvps.org
>.
Jay Freedman - 10 Mar 2005 18:52 GMT
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

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> This is the first time I have used Access to open a Word
> document and insert data from Access into Word and my
[quoted text clipped - 62 lines]
>> Microsoft Word MVP         FAQ: http://word.mvps.org
>> .
anonymous@discussions.microsoft.com - 10 Mar 2005 20:33 GMT
Hi Jay,

A million thanks.  The code worked like a charm!!!

Cindy

>-----Original Message-----
>Hi Cindy,
[quoted text clipped - 120 lines]
>>  oSec.Footers(wdHeaderFooterPrimary).Range.Text = "text"
>> Load page number within the footer

oSec.FooterswdHeaderFooterPrimary).PageNumbers.Add.Alignmen
>> t = wdAlignPageNumberCenter
>>
[quoted text clipped - 37 lines]
>
>.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.