Word Automation: Write RTF text without going through clipboard

青春壹個敷衍的年華 提交于 2019-12-03 13:17:33

问题


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?


回答1:


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




回答2:


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

RichTextBox r = new RichTextBox();
r.Rtf = strRTFString;
Console.WriteLine(r.Text);


来源:https://stackoverflow.com/questions/22326/word-automation-write-rtf-text-without-going-through-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!