Using the FormattedText property of MS Word rather than the clipboard to pass RTF text to Word
Asked Answered
M

0

6

I want to spell/grammar check the content of a TDBRichEdit component using Word automation (early binding) while preserving the RTF-formatting of the source during the process. The obvious way of doing it is to use the Clipboard as follows:

  1. DBRichEdit.SelectAll;
  2. DBRichEdit.Lines.CopyToClipboard;
  3. WordDoc.Content.Paste;
  4. do the spell/grammar check;
  5. WordDoc.Content.Copy;
  6. DBRichEdit.PasteFromClipboard.

It works but I consider this as bad programming as it messes with the clipboard content (which might be annoying).

I've just stumbled onto the FormattedText property of the Range object, which allows copy and paste RTF-formatted text (including paragraph formatting) by assigning it to a range object rather than by passing it through the clipboard. It is declared as

property FormattedText: Range read Get_FormattedText write Set_FormattedText;

in WordXP.pas and I don't know how to assign it the content of the TDBRichEdit. Is the use of this property possible? I have also tried streaming to the word document but to no avail.

How could I assign the content of the TDBRichEdit component (DBRchEdit.Lines) to the Word document (and recover it back after the spell/grammar check) without using the Clipboard?

Manoff answered 8/7, 2013 at 15:50 Comment(4)
FWIW the FormattedText property doesn't help you, because you have to get your formatted text into a range in order to use Range.FormattedText. If DBRichEdit also provides WordML format (e.g. Word 2003 format or FlatOPC) then you could consider using Range.InsertXML. If not, I think you either have to find a way to convert the RTF, or put the RTF in a disk file, then insert it via Range.InsertFile. If you are working directly with the OpenXML on disk, Altchunks might work, but I don't think that is possible when you are working via the object model.Longeron
Just build a Flat OPC in memory, and AltChunk the RTF file in -- (you can do it all in memory -- easier if you have the OpenXML SDK, but you could also do this with a good template [say as an embedded resource] and XLinq). You can then call Range.InsertXml on your Flat OPC.Janetjaneta
Why do you need RTF formatting for spell checking?Subarid
ActiveDocument.Range.Text is variable that get acsess to text of documentAccompanist

© 2022 - 2024 — McMap. All rights reserved.