简体   繁体   中英

How do I copy formatted HTML string into a Clipboard for paste using C#?

My ultimate objective is to Insert formatted rich text into Outlook inspector (eg AppointmentItem, MailItem, etc). I asked this question on MSDN forums, and somebody replied I should use Clipboard to its copy contents, then Paste it in the window using hte target formatting I want to use.

I have the following HTML string

<html><body><table><tr><td>iPhone</td><td>Telephone#</td></tr></table></body></html>

And I have the following C# code

Clipboard.SetText(insertString, TextDataFormat.Html);

//....
//code to get Word.Range in my inspector item (inconsequential to this question)
//....

selected.Paste();

The problem is that the Clipboard doesn't insert the text, or throws a COM Failed exception in the code.

使用Paste()时出现COM异常

ANSWER thanks to jlew

 HtmlFragment.CopyToClipboard(insertString);
 selected.Paste();

HtmlFragment code found here: http://blogs.msdn.com/b/jmstall/archive/2007/01/21/sample-code-html-clipboard.aspx

Unless something has changed since the olden days (.NET 2, which was the last time I tried this) you need to add the proper clipboard header lines before putting it on the clipboard, as described here:

http://blogs.msdn.com/b/jmstall/archive/2007/01/21/html-clipboard.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM