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

Word automation text formatting lost

Status
Not open for further replies.

carnsoft

Programmer
Aug 28, 2009
3
GB
I'm using VFP9 and trying to do a mail merge on the fly in Word. I have a Word document with some basic formatting (bold, etc).
I can open the document and I store oRange.TEXT to a variable but when I try to reinsert the text, the formatting is lost.

I use this code to get the original text:

Code:
oDocument = GETOBJECT(cDocument)
oRange = oDocument.RANGE()
cOriginal = oRange.TEXT

Then I open Word, add a new document and use this code to setup the new document:

Code:
oWord = CreateObject("Word.Application")
oDocument = oWord.Documents.ADD()
SELECT tempdata
SCAN
	oRange = oDocument.RANGE()
	oRange.TEXT = oRange.TEXT + cOriginal
...
ENDSCAN

The text from the original document appears in the new document, but all the formatting is removed. All the text appears as normal.

I wondered if I could try opening the original document, copying the text then pasting it into the new document. This is my first foray into Automation, so my knowledge isn't that great.

Thanks in advance,

Neil

 
The first piece of advice on ANY automation task is to attempt the operation manually in the Office product (Excel, Word, etc) without any VFP involved while recording the operations as a Macro. When done, stop recording the Macro.

Then you can go into the Macro and see what steps the application did itself in order to perform the task(s). Those are the steps that you will want your Automation to duplicate.

When I last did some Word automation I did as you 'wonder about'. I opened the virgin 'template' and then saved it to the final (albeit empty) document. And closed the 'template' document. Next I modified the contents of the final document as needed.

Admittedly I have done MUCH more Excel automation than I have Word automation so others may have a much better approach.

You might also want to get Tamar's excellent reference book:
Microsoft Office Automation with Visual FoxPro

Good Luck,
JRB-Bldr
 
Thanks JRB-Bldr, I'll create the macro and see how I get on from there.

I'll also look at getting the book and play about some more.

Neil
 
Neil,

I was a bit hasty.

I still think you should use FormattedText, but you can't simply concatenate some existing text to it. In other words, this line won't work:

Code:
oRange.FormattedText = oRange.FormattedText + cOriginal

You will need to so something like this instead:

Code:
oRange.FormattedText.InsertAfter(cOriginal)

It might need a bit of experimenting to get it exactly right, but I think the above will put you on the right track.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks for your help.

I changed tack slightly and I'm happier with the result. I used macros in Word to work out the best way forward, so thanks to JRB-Bldr for that advice.

I will have a play about with FormattedText, no doubt that will crop up at some point!

Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top