Discussion:
Word Automation
(too old to reply)
Andy Trezise
2010-05-01 15:52:50 UTC
Permalink
Hi All

Having fun (probably not the correct word) with Word Automation

I simply want to open a document, select the text, make changes to the text,
then write those changes out as a new page (maybe doing this repeatedly as I
skip through my database) before saving the document wih a new name.

I can get it to work by working backwards (i.e. inserting pages 'before' the
current one) but really I should be inserting pages at the end of the
document as I skip through the data in chronological order.

This is my code:

GO TOP IN "MYDATA"
oWord = CREATEOBJECT("Word.Application")

oWord.Documents.Open("c:\varcarme letterhead.dot",.f.,.t.)

oRange = oWord.ActiveDocument.Range()

cText = oWord.ActiveDocument.Range.Text

cString = CHRTRAN(cText,"%ACCOUNT%",account_number)

oWord.ActiveDocument.Range.Collapse(0)

oRange.InsertBreak(2) && This inserts a page before the currect position

oWord.ActiveDocument.Range.InsertBefore(cString)

SKIP IN "MYDATA"

cString = CHRTRAN(cText,"%ACCOUNT%",account_number)

oRange.InsertBreak(2) && This inserts a page before the currect position

oWord.ActiveDocument.Range.InsertBefore(cString)

oWord.Visible = .t.

Just wondered if anyone had done this kind of thing before.
Dan Freeman
2010-05-01 16:43:38 UTC
Permalink
Post by Andy Trezise
Just wondered if anyone had done this kind of thing before.
Yeah. It's called mailmerge and it's built-in to Word. <g>

Dan
Andy Trezise
2010-05-10 20:06:31 UTC
Permalink
Very constructive Dan

I just wanted a litte help with how to do it via Foxpro
Post by Dan Freeman
Post by Andy Trezise
Just wondered if anyone had done this kind of thing before.
Yeah. It's called mailmerge and it's built-in to Word. <g>
Dan
Bernhard Sander
2010-05-05 09:33:16 UTC
Permalink
Hi Andy,
Post by Andy Trezise
Having fun (probably not the correct word) with Word Automation
There are not only several ways to skin a fox, but also to skin a word ;-)
Post by Andy Trezise
I simply want to open a document, select the text, make changes to the text,
then write those changes out as a new page (maybe doing this repeatedly as I
skip through my database) before saving the document wih a new name.
I can get it to work by working backwards (i.e. inserting pages 'before' the
current one) but really I should be inserting pages at the end of the
document as I skip through the data in chronological order.
GO TOP IN "MYDATA"
oWord = CREATEOBJECT("Word.Application")
oWord.Documents.Open("c:\varcarme letterhead.dot",.f.,.t.)
oRange = oWord.ActiveDocument.Range()
cText = oWord.ActiveDocument.Range.Text
Why don't you use oRange.Text instead of oWord.ActiveDocument.Range.Text?
Post by Andy Trezise
cString = CHRTRAN(cText,"%ACCOUNT%",account_number)
oWord.ActiveDocument.Range.Collapse(0)
Use oRange.Collapse(0) and go on working with oRange instead of
oWord.ActiveDocument.Range.
oRange is the range, that you grow, shrink, move, add text to; something like an
invisible Cursor in your Word document.
oWord.ActiveDocument.Range is always the whole document.

For debugging purposes, make the word document visible, add some
oRange.Select
every now and then in your program and watch the word document to see what is
happening and where you are working at the moment while stepping through your
program.

Regards
Bernhard Sander
Andy Trezise
2010-05-10 20:08:06 UTC
Permalink
Thanks for your comments Bernard

I've managed to do it by collapsing the range before every insert.

Your comments are greatly appreciated
Post by Bernhard Sander
Hi Andy,
Post by Andy Trezise
Having fun (probably not the correct word) with Word Automation
There are not only several ways to skin a fox, but also to skin a word ;-)
Post by Andy Trezise
I simply want to open a document, select the text, make changes to the
text, then write those changes out as a new page (maybe doing this
repeatedly as I skip through my database) before saving the document wih
a new name.
I can get it to work by working backwards (i.e. inserting pages 'before'
the current one) but really I should be inserting pages at the end of the
document as I skip through the data in chronological order.
GO TOP IN "MYDATA"
oWord = CREATEOBJECT("Word.Application")
oWord.Documents.Open("c:\varcarme letterhead.dot",.f.,.t.)
oRange = oWord.ActiveDocument.Range()
cText = oWord.ActiveDocument.Range.Text
Why don't you use oRange.Text instead of oWord.ActiveDocument.Range.Text?
Post by Andy Trezise
cString = CHRTRAN(cText,"%ACCOUNT%",account_number)
oWord.ActiveDocument.Range.Collapse(0)
Use oRange.Collapse(0) and go on working with oRange instead of
oWord.ActiveDocument.Range.
oRange is the range, that you grow, shrink, move, add text to; something
like an invisible Cursor in your Word document.
oWord.ActiveDocument.Range is always the whole document.
For debugging purposes, make the word document visible, add some
oRange.Select
every now and then in your program and watch the word document to see what
is happening and where you are working at the moment while stepping
through your program.
Regards
Bernhard Sander
Loading...