Click here to Skip to main content
15,867,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm automating Word to create a new document where I'm building the document from blob fields within a Firebird database. Everything works fine until I try to insert text followed by RTF content while preserving the RTF formatting. What I'm trying to end up with might look like this.

III.3a) The is the RTF formatted text.

The 'III.3a' is plain text from a VarChar field but the rest is RTF stored in a blob. When trying to get them both to appear on the same line, the RTF seems to be overwriting the plain text.

I can do this in C# or Delphi; language doesn't matter. Here's some Delphi code with the RTF stuff having been read into a Delphi TRichEdit component. WordDocument is a Delphi TWordDocument component.

WordDocument.Paragraphs.Last.Range.InsertAfter('III.3a) ') ;
Rtf.SelectAll ;
Rtf.CopyToClipboard ;
WordDocument.Paragraphs.Last.Range.Paste ;

Thanks for any help.... If you can provide a working snippet, I'll name my next 4 children after you.
Posted

Suggestion: glue the VarChar and RTF together inside your TRichEdit then paste the lot in one hit. At least you'll have the Delphi IDE to debug it! ;P

Another point (rereading after posting :-O ) the behaviour sounds like the III.3a is selected in Word when the paste occurs, so it's a replace rather than insert. Any way of checking what's selected in Word immediately before pasting? If it's the III.3a, put something after it and select that for replacement by paste.
 
Share this answer
 
v2
Thanks for your reply. Inserting something after the III.3a was an excellent suggestion (one that I hadn't tried) but didn't work. It appears that "WordDocument.Paragraphs.Last.Range.Paste" pastes from the beginning of the line overwriting anything previously inserted on that line. I need to be able to paste AFTER existing text on the line and that's what I'm not figuring out. :confused:
 
Share this answer
 
;) ;) ;)
I got it!!! I changed "WordDocument.Paragraphs.Last.Range.Paste" to "WordDocument.Bookmarks.Item(endOfDoc).Range.Paste" where endOfDoc = '\endofdoc'. Now it pastes the RTF at the end of the document without overwriting what been previously inserted on the line.
 
Share this answer
 
Comments
Peter_in_2780 13-Aug-10 22:06pm    
Good to hear. Would you like to mark your question as answered? Cheers, Peter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900