Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word: updating Fields

Status
Not open for further replies.

andre65

Programmer
Jan 19, 2003
95
NL
I have a Word document containing the fields [Page] and [NumPages] to create [X of Y pages]. During automation I create one or more pages, so at the end I want to update the fields.

Updating field [Page] works fine.
Updating field [NumPages] does not work.

I have the following code:

*-- First repaginate, don't do this in background
this.oWord.options.pagination = .f.
this.oDocNew.repaginate
*-- Now update fields, if any
if this.oDocNew.fields.count>0
for i=1 to this.oDocNew.fields.count
this.oDocNew.fields(i).update
endfor
endif

How can i get [NumPages] to work?

Thanks for any reply.
André
 
Andre,

Word has an "X of Y" formula to do this for you. It would go in the page footer.

Here is the syntax I have used for this:
Code:
oDoc.Sections(1).Footers(1).Range.Select
WITH oWord
  .NormalTemplate.AutoTextEntries("Page X of Y").Insert(.Selection.Range).ParagraphFormat.Alignment = _wdAlignParagraphRight
  .ActiveWindow.Panes(2).Close()
  .ActiveWindow.ActivePane.View.Type = _WDPRINTVIEW
ENDWITH

You need the last 2 lines to put Word back into a normal view.


Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Stewart,

The [Page X of Y] autotext field is eventually the same as using [Page] and [NumPages] fields. So my questions stays the same.
By the way: I want the user have the flexibility to put Fields anywhere in the document, not only in the footer. The document is created by the user itself, and serves as a template which is used by my app.

Gr,
André
 
Andre,

I've looked a bit further. You can insert Page X of Y anywhere in a document using the Insert\Autotext\Header/ Footer\Page X of Y menu option.

It seems the only automatic updating option is to update before printing which is on the the Print tab of the Tools\Options form.

However if you select the whole document and press F9, then the fields update themselves.

So your users should be able to create the template and then your app can update the fields when creating a document from the template.

Here's the VB syntax that I got from the macro recorder:
Code:
    Selection.WholeStory
    Selection.Fields.Update

Does that help?

Greetings,

Stewart
 
Stewart,

It almost works ...
In a document where 2 pages are created, only the first page shows [1 of 2 pages], the second page shows [2 of 1 pages].

This is the code I have tried:

this.oDocNew.activate
this.oWord.selection.wholeStory
this.oWord.selection.fields.update

and

this.oDocNew.activate
loRange = this.oDocNew.range(;
this.oDocNew.range.start,;
this.oDocNew.range.end)
loRange.fields.update

So, how do we get the second page work...

André

 
Andre,

Oh dear! I think "WholeStory" is a Word constant.

I just created a 2 page document with the "Page X of Y" field on both pages. When I ran this code...
Code:
odoc = oword.ActiveDocument
odoc.Select()
oword.Selection.Fields.Update()
...Word correctly updated the page numbers on both pages.

Stewart
 
Stewart,

The VBAWRD helpfile says that WholeStory is a method.

I tried your code suggestion.
Still the same result. Second page displays [page 2 of 1].

In your example, do you have a hard pagebreak. I use in code:
this.oWord.selection.insertBreak(wdPageBreak)

Maybe this blocks the update ... i'm only quessing.

André
 
Andre,

Oh yes, so I see.

I've been creating the document manually using Word having made a (possibly wrong) assumption from you saying...
The document is created by the user itself, and serves as a template which is used by my app
...that the users would be doing the same.

I then went to VFP and used
Code:
oWord = GETOBJECT(,"Word.Application")

Are you doing it that way or doing the whole thing with automation?

By the way, working manually, I found that entering a Page Break (with Ctrl+Enter) doesn't affect it.

Having tried the whole thing with automation, I also find that the update doesn't work - it blanks out the page numbers for me!

Does that get you any further?

Stewart
 
Stewart,

As I understand, you now have the same problem as I have?

I use CREATEOBJECT not GETOBJECT, but that's more or less the same.

The user can create it's own template, but that wil be one page. My app determines if there are more pages neccessary.

André
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top