I want to put rich text in HTML on the clipboard so when the users paste to Word, it will include the source HTML formatting.
Using the Clipboard.SetText method doesn’t work.
Also, I would like that if users paste into a rich editor like Word it will paste formatted text, and if they paste into a plain editor like Notepad it will paste plain text.
When setting HTML text, you need to provide a header with additional information to what fragment of the html you actually want to paste while being able to provide additional styling around it:
With the header (and correct indexes), calling
Clipboard.SetTextwithTextDataFormat.Htmlwill do the trick.To handle HTML and plain text pastes, you can’t use the
Clipboard.SetTextmethod, as it clears the clipboard each time it’s called; you need to create aDataObjectinstance, call itsSetDatamethod once with HTML and once with plain text, and then set the object to clipboard usingClipboard.SetDataObject.Update
See “Setting HTML/Text to Clipboard revisited” for more details and ClipboardHelper implementation.