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

VFP9 & Word Office automation

Status
Not open for further replies.

Goofus828

Programmer
Nov 9, 2005
127
US
Hi all,
I am using VFP 9sp2 to automate the creation of word documents.

I'm creating documents based on groups and locations.
Each Group will have several locations.
Each location will be on a separate page.
each group could have several pages.

I need page breaks when locations change.

Code:
oWord = CREATEOBJECT( "Word.Application" )
oWord.Documents.ADD()
oDoc = oWord.ActiveDocument
** Set the margins to be .3"
oDoc.PageSetup.BottomMargin = 21.5
oDoc.PageSetup.LeftMargin   = 21.5
oDoc.PageSetup.rightmargin  = 21.5
oDoc.PageSetup.topmargin    = 21.5
** Set landscape
oDoc.PageSetup.ORIENTATION = 1

** Delete any footer information
oWord.ActiveDocument.Sections( 1 ).Footers.ITEM( 1 ).RANGE.DELETE( 1,1 )
** This is because it is not know who's machine
** will run code and could have custom Normal.dot"

.... get data ....

oRange = oWord.ActiveDocument.RANGE()
xLine = "text to insert"
oRange.InsertAfter( xLine + CRLF )

** Need to insert new page.
oPage.InsertBreak( wdPageBreak )

The cursor is at the top left of the page and problem is that when I issue the new page break, it is inserted at the top of the document and not at the bottom.
And then when I go to add text on the new page, the text is deleted.

I'm thinking the oRange is incorrect and how do I move the cursor to the bottom of the page so that the insert works and nothing is selected.

Thanks!!!!!!


 
Try collapsing the range and the using InsertBreak on the range, not the page.

Tamar
 
Have you tried doing this work manually within Excel without involving VFP at all?

If you do this and record the actions as a Macro, you can then go into the Macro code and examine it to see what you should be doing with your VFP Automation.

Good Luck,
JRB-Bldr
 
Tamar, thanks for the reply. I could not get that to work at all.

JBR - these documents are for a client. Yes Excel is much easier to code for.

I cheated by using the MoveUp( 7,7 ) before the insert page then another MoveUp( 7, 7). This moves the cursor up seven paragraphs.
It accomplishes what I needed.

Thanks for the reply s.
 
Where does oPage come from? Collapse range as Tamar said.

Code:
oRange.InsertAfter( xLine + CRLF )
oWord.ActiveDocument.Content.Select()
oWord.Selection.Collapse( wdCollapseEnd )
** Need to insert new page.
oWord.Selection.InsertBreak( wdPageBreak )

Cetin Basoz
MS Foxpro MVP, MCP
 
"JBR - these documents are for a client. Yes Excel is much easier to code for."

Perhaps you misunderstood.

I was not suggesting that you DO the work in Excel as a final result.

I was suggesting that you initially create a Macro within Excel and examine the VBA code so as to determine what your VFP Automation code needed to do and how it needed to do it - then eliminate the temporary Excel Macro.

This general approach is what we all (those of us who do a LOT of VFP Automation code) use to determine what to do within our VFP Automation code.

The documents being for a client is a moot point. With the appropriately working Automation code in VFP it can run on your client's site over-and-over and produce the desired results.

Good Luck,
JRB-Bldr
 
JRB - he wants Word, not Excel. Recording a macro can be a good way to get started, though it tends to push you to code that isn't as good as what you can write.

Cetin - there's no reason to deal with the Selection object. That just adds slow UI.

Goofus828 - Cetin's pointing you in the right direction. Try this:

Code:
oRange.Collapse(wdCollapseEnd)
oRange.InsertBreak(wdPageBreak)

Tamar
 
I don't think it would work with just:

Code:
oRange.Collapse(wdCollapseEnd)
oRange.InsertBreak(wdPageBreak)

Then you need to add some code like Move, Goto (I chose select()).

PS: I didn't see that Selection slows down UI.

Cetin Basoz
MS Foxpro MVP, MCP
 
I haven't tested this particular case, but collapsing the range to a point at the end and then inserting a break should work.

In general, automation is faster when you don't touch the Selection object.

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top