11

I am trying to replace the current selection in Word (2003/2007) by some RTF string stored in a variable.

Here is the current code:

Clipboard.SetText(strRTFString, TextDataFormat.Rtf)
oWord.ActiveDocument.ActiveWindow.Selection.PasteAndFormat(0)

Is there any way to do the same thing without going through the clipboard. Or is there any way to push the clipboard data to a safe place and restore it after?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Vincent
  • 22,366
  • 18
  • 58
  • 61

2 Answers2

14

Put the RTF in a file instead of the clipboard, then insert from the file, e.g.

Selection.InsertFile FileName:="myfile.rtf", Range :="", _ ConfirmConversions:=False, Link:=False, Attachment:=False

Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
  • Thanks Joel. To make this answer more broadly useful than the context of the original question, let me add that the `InsertFile` method is also on the `Range` property (which is accessible, for example, on `Word.Document`, `Word.Document.Sections` elements, etc.) – WizzleWuzzle Mar 14 '16 at 22:28
-3

You can use a RichTextbox to convert RTF to text or vice versa.

RichTextBox r = new RichTextBox();
r.Rtf = strRTFString;
Console.WriteLine(r.Text);
samjudson
  • 56,243
  • 7
  • 59
  • 69