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

how to put rtf content into word docoument 4

Status
Not open for further replies.

Stefan5627

Programmer
Jul 23, 2002
71
NL
I use a richtextcontrol to put rtf text in a memo field. How can i insert this rtf content into a word document?
I've tried putting it in the clipboard and pasting it in the worddocument, but what i get is the plain text with the rtf formatting tags.

Does anybody know the right syntax to put rtf content into word?
 
Stefan5627

Use the SaveFile() method of the Activex control to save the content of an RTF control to an RTF document, it will keep the formatting.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
thanks Mike, this way I can save it to a file, open the file and copy and paste it into the document i want, but what i'm actually looking for is a command like:

odoc.myrange.text = myrtfcontent

If i use this my worddocument shows the rtf tags and not the formatted text, so i'm looking for a command that can put the rtf content of a memofile directly into word.
Do you know of such a command?

Stefan
 
I think you can have either a RTF document, or a Word document, not a combination of both in one file.

To get RTF to look good in Word, it has to be an RTF file.

To convert an RTF file to Word, just saveas() an RTF document (as Mike suggests), and then automate an instance of Word, open the rtf file and perform a saveas() Word.

Brian
 
Brian, i do not need to do a word saveas, this is the way i do it:

Code:
oWord = createobject("word.application")
oTemplateDoc = oword.documents.open("mytemplate.doc")
* this is a document the user can make, filling it with tags
* that need to be replaced with the content off some memofields
oform = createobject("form")
oform.addobject("ortfcontrol", "olecontrol", "RICHTEXT.RichtextCtrl")
oform.ortfcontrol.textrtf = mytable.mymemo
oform.ortfcontrol.savefile("temp.doc")
oTempdoc = oword.documents.open("temp.doc")
findrange(@omyrange)   && finds a range in oTemplatedoc that needs to be replaced
oTempdoc.content.copy
omyrange.paste

This way i can replace a tag in oTemplatedoc with the rtf content in mytable.mymemo
However i think there must be a more direct approach to putting rtf text in a word document than the way i'm doing here (storing the rtf content in a richtextcontrol, saving it to file, opening the file in word and copying the content of that wordfile)
 
Stefan5627,

1. If you want to use the Windows clipboard:

Go to and look at the WinAPI clipboard functions. If you're going to move RTF to the clipboard, you need to use a WinAPI call (vs. VFP's _cliptext) to tell the clipboard that the type of data you're inserting is RTF vs. TEXT.

2. The way I do what you want to do is save my user's template doc as an RTF file and then use VFP's TEXTMERGE functionality to expand out the <<expression>>'s in the resulting RTF. I save the result to a file with a .DOC file extension (even though the contents of this file are just RTF codes) and Word opens this file fine (it looks at the file header and determines its RTF vs. DOC binary and makes the adjustments automatically). I find this easier and faster than Word automation. In fact, your users don't even need to have Word installed on their machines - you can use the free MS Word Viewer to display the files you generate - ready for reading or printing.

The only downside to keeping Word files in an RTF vs. DOC file format is that the DOC file binary format is more compressed - ESPECIALLY if your document has images. If RTF file size is an issue then you can:

a. Use Word automation to load your RTF file and save it in a DOC file format

b. Compress your RTF files - they will compress to a similar size as DOC files even though DOC files start out smaller.

Hope this helps!
Malcolm
 
Stefan5627,

A colleague of mine has done a lot of research in using the clipboard to transfer RTF between VFP and Word. He is in the prcess of writing an article on how to do it.

If you would like a draft copy of the article, I can email it to you.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Malcolm and Mike L,
thank you for your reply. As a result of a google search on the problem i found some delphi code which seemed to use the clipboard as well, that looked promissing. It too used a function to 'format' the clipboard content. I did not know there is an API function for that. Does that function work on all versions of windows?
Malcolm, it took some searching to get to the right page, but it's an excellent site (Yes Mike, i would appreciate a draft copy very much, you can send it to stefan@po-software.nl
Will the final version be posted as a faq on tek-tips?
I've got it all working now with the code i've shown above but using the clipboard as you suggested looks better.

once again, thank you both

Stefan
 
Stefan5627,

Congratulations on getting your code working. And another plug for the news2news.com/vfp web site. This site is a treasure trove of content. I learned a lot of cool techniques by copying and running all the sample code. Well worth subscribing ... a bargain!

Note that I am not in anyway affiliated with this site.

Malcolm
 
Stefan5627,

Thanks for posting that reference to Phil's aritcle. I had meant to post it myself, but kept forgetting.

Phil has done a tremendous amount of work on this, and his article is an excellent contribution.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
I am looking for a reference of VB commands that I can use in FoxPro to access objects in a Word document. For example, I want to fill in a textbox (called "textbox1") in a word document. No matter what syntax I use to reference the object, it gives an error. I can create a word object, insert text, change fonts, etc., but trying to find a list that I can reference with the correct syntax has been futile. Does anybody know of such a reference?
 

I am looking for a reference of VB commands that I can use in FoxPro to access objects in a Word document

VB commands do not work in FoxPro.

Have you looked at the faq section in forum184? There are samples of Word automation code that may help you get started.

Mike Gagnon

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

You might get a better response if you start a new thread. Anyone seeing the title of this thread would not relate it to your problem.

Having said that, have you tried the VBA object browser? In Word, hit Alt+F11 (to get into VBA), then hit F2 to open the browser. Select Word from the combo box near the top of the screen. You will then be able to explore the Word object model quite easily.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top