Click here to Skip to main content
13,043,154 members (61,762 online)
Rate this:
Please Sign up or sign in to vote.
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 12-Aug-10 2:04am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

;) ;) ;)
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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 Aug 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100