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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Automation & number of pages returned 1

Status
Not open for further replies.

OhioBill

Programmer
Nov 10, 2003
66
US

I need to open a document in Word and get the number of pages. My documents have only one section. The problem is that if the document has more than two pages, the number of pages returned is 2 (or 1, as the case may be). Any number of pages greater than two returns only two, not the correct number.

I discovered that if I waited a few seconds for the entire document to load, the correct number of pages was returned. I'm using the wait verb to accomplish this, but I'm thinking there must be a better way.

I've tried setting autoyield to .f. and using the doevents verb, but it either doesn't work or I'm doing something wrong with it.

I'm using VFP 7, Word 2000, Windows 2000.


stripped down code example:

go_WordApp = createobject("word.application")

lc_DocFileName = "c:\five_page_document.doc"

Store .F. To GV_ConfirmConversions, GV_ReadOnly, GV_AddToRecentFiles

go_WordApp.Documents.Open(lc_DocFileName, GV_ConfirmConversions, GV_ReadOnly, GV_AddToRecentFiles)

* If I omit the wait stmt the number of pages returned is
* two. Adding the wait stmt allows the
* correct number of pages to be returned, in this case five.

Wait "" Timeout 3

ln_number_of_pages = go_WordApp.ActiveDocument.Sections(1).Range.Information(3)

 

You could use inkey(3). The problem is a bit like when you automate Internet Explorer, and load a page, it takes a few seconds to load it.
You could use
Code:
INKEY(3)
 
Or 
DECLARE Sleep IN Win32API INTEGER nMilliseconds
* Pause for 5 seconds

nSleepSeconds = 5
nMilliseconds = nSleepSeconds * 1000
=Sleep(nMilliSeconds)


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
The wait is unnecessary...you need to repaginate the document prior to retrieving the page count information, here's a working example based on your code above:
Code:
go_WordApp = createobject("word.application")

lc_DocFileName = "c:\five_page_document.doc"

Store .F. To GV_ConfirmConversions, GV_ReadOnly, GV_AddToRecentFiles

go_doc = go_WordApp.Documents.Open(lc_DocFileName, GV_ConfirmConversions, GV_ReadOnly, GV_AddToRecentFiles)

go_doc.Repaginate [COLOR=green]&& This will fix the problem[/color]

ln_number_of_pages = go_WordApp.ActiveDocument.Sections(1).Range.Information(3)

MESSAGEBOX("Total Pages: " + ALLTRIM(STR(ln_number_of_pages)))

go_doc.close()
go_WordApp.Quit
RELEASE ALL
CLEAR ALL

boyd.gif

 
Mgagnon, Criagsboyd, thanks for the help!

I used the repaginate method and it worked great.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top