Reputation: 12794
I'm trying to programatically generate and copy formatted text to the clipboard for the purposes of pasting it into an e-mail. The only formatting I want to copy is line breaks and bold text. I want it to use the e-mail's current or default font.
Licensed to: XXXXX
License key: XXXXX
My attempts at HTML produce the correct formatting in Windows Live Mail, but with it's own choice of font.
Dim Text As String =
"Version:0.9" & vbCrLf &
"StartHTML:00000097" & vbCrLf &
"EndHTML:00000343" & vbCrLf &
"StartFragment:00000243" & vbCrLf &
"EndFragment:00000308" & vbCrLf &
"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">" & vbCrLf &
"<HTML><HEAD><TITLE>Software License</TITLE></HEAD><BODY>" & vbCrLf &
"<!--StartFragment -->" & vbCrLf &
"<P><B>Licensed to:</B> XXXXX<BR /><B>License key:</B> XXXXX</P>" & vbCrLf &
"<!--EndFragment -->" & vbCrLf &
"</BODY></HTML>"
Clipboard.SetText(Text, TextDataFormat.Html)
My attempts at RTF paste in WordPad properly, but pasting in Windows Live Mail simple does nothing.
Dim Text As String =
"{\rtf1\ansi " &
"\b Licensed to:\b0 XXXXX\par " &
"\b License key:\b0 XXXXX\par " &
"}"
Clipboard.SetText(Text, TextDataFormat.Rtf)
Does anyone know a solution to copy limited formatting to the clipboard.
Upvotes: 2
Views: 1069
Reputation: 32568
This is really format-specific (and in some ways application specific). You've found out how to do it for two cases, but should be aware of why it is working as it is:
When you attempt to paste data from the clipboard in a application (say, outlook), that application first queries the clipboard to see what formats are available, and will choose the available format "best" for it (as determined by Outlook's developers). If no formats are available that work for that application, then it acts as if no data is on the clipboard. This explains why wordpad can handle RTF data, but live mail does not.
What you can (and should) do, is put data on the clipboard in multiple formats to allow other apps to pick and choose.
Instead of using Clipboard.SetText()
, use Clipboard.SetDataObject()
, and fill a DataObject
with multiple formats
Dim htmlText as String = ''your working html
Dim rtfText as String = ''your working rtf
Dim data As New DataObject()
data.SetText(htmlText, TextDataFormat.Html)
data.SetText(rtfText, TextDataFormat.Rtf)
''and possibly:
data.SetText(plainText, TextDataFormat.Text)
Clipboard.SetDataObject(data)
Upvotes: 4