richtext to outlook message

M

mrstrong

Gday All,

I have a rich text box in a winforms app that I am attempting to copy
the rich text (with formatting in tact) to an outlook message that I
have created using Microsoft.Office.Interop.Outlook...

However copying the rtf straight to the message does not work - the
rtf code is displayed, rather than the formatted text.

There does not appear to be an exposed rtf property to copy to (which
would be ideal), unless you use something like Redemption by the looks
of it which costs money and is not an option for me right now.

So, then I tried to create a MSWord document, copy the rich text to
this, save as html (in MSWord), then open up the saved html file and
the generated html into the ..HTMLBody of the email message. I
thought for sure this would work, but the MSWord generated HTML is not
compatible with the Outlook HTML it would seem and as such some
characers are not recognised and are displayed as '?' - not pretty.

So I am wondering if anyone knows a way to copy rich text and have it
display correctly in an Outlook message??

Thanks in advance,

Peter
 
K

Kalpesh

Peter,

Check MailItem 's BodyFormat property
Note: I have just looked up msdn & didn't try writing any code to do
it

Does that work?

Kalpesh
 
P

Peter Duniho

[...]
So I am wondering if anyone knows a way to copy rich text and have it
display correctly in an Outlook message??

Not knowing the Outlook interop API, I can't tell you if there's a way to
initialize an Outlook message with the actual RTF data directly.

However, the RichTextBox control does have a Copy() method that will copy
the current selection to the clipboard. I'm not a big fan of
programmatically modifying the clipboard except when the user explicitly
asks you to, but in this case it might be an acceptable compromise if you
can't find a better way to do it directly.

Of course, this would depend on the Outlook interop API including a method
that allows you to paste into an Outlook message from the clipboard.
Again, I don't know whether that exists. But it seems that might be more
likely to exist than the other. :)

By the way, if you find yourself looking again at your other workaround,
the "HTML to Word to Outlook" technique, you might explore trying to fix
the '?' characters with explicit setting of character encodings or fonts,
either of which might lead to characters that are either unrecognized,
incorrectly saved, or undisplayable in Outlook.

Pete
 
M

mrstrong

Peter,

Check MailItem 's BodyFormat property
Note: I have just looked up msdn & didn't try writing any code to do
it

Does that work?

Unfortunately no - I was already doing that. Here is the code where I
set the format for the message:

msg.Message.BodyFormat =
Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;

Then I set the HTMLBody property of the message to be a string
containing the HTML that I have pulled out of the html file that I
have converted from MSWord..

Thanks,

Peter
 
M

mrstrong

[...]
So I am wondering if anyone knows a way to copy rich text and have it
display correctly in an Outlook message??

Not knowing the Outlook interop API, I can't tell you if there's a way to
initialize an Outlook message with the actual RTF data directly.

However, the RichTextBox control does have a Copy() method that will copy
the current selection to the clipboard. I'm not a big fan of
programmatically modifying the clipboard except when the user explicitly
asks you to, but in this case it might be an acceptable compromise if you
can't find a better way to do it directly.

Of course, this would depend on the Outlook interop API including a method
that allows you to paste into an Outlook message from the clipboard.
Again, I don't know whether that exists. But it seems that might be more
likely to exist than the other. :)

By the way, if you find yourself looking again at your other workaround,
the "HTML to Word to Outlook" technique, you might explore trying to fix
the '?' characters with explicit setting of character encodings or fonts,
either of which might lead to characters that are either unrecognized,
incorrectly saved, or undisplayable in Outlook.

Thanks Pete - I tried that too but the outlook message does not have a
Paste() method unfortunately.

Your other suggestion about character conversion is my last resort,
but I wouldnt have thought it would be this hard to simply copy some
rich text into an email!!

Regards,

Peter
 
P

Peter Duniho

Thanks Pete - I tried that too but the outlook message does not have a
Paste() method unfortunately.

Really? That's surprising. I know that Excel and Word automation both
support clipboard operations, via paste methods. I don't recall off the
top of my head whether those methods are in the document classes (e.g.
Worksheet) or part of the application API (e.g. Application). It's
possible it's the latter, which would require activating the document of
interest before executing the paste. Did you only look in the MailItem
class for a paste operation? If so, you might also want to check to see
if the Outlook application class has the method instead.

Sorry I'm being so vague...I wish I had more experience with what you're
doing and could offer better advice. But I'm hoping I can at least ensure
that you haven't overlooked something useful.
Your other suggestion about character conversion is my last resort,
but I wouldnt have thought it would be this hard to simply copy some
rich text into an email!!

Well, I don't know that it is. I just don't know that it isn't, either.
:)

By the way, with respect to the suggestion from Kalpesh, I'm wondering if
there's an RTF format you can use with the BodyFormat property instead of
"olFormatHTML" (maybe it's called "olFormatRTF" :) ). Then you could set
the Body property to the RTF you get directly from the RichTextBox rather
than going through Word. Less conversion is always better and maybe that
would address the issues you're running into, assuming such a format is
supported. Who knows? It might even avoid the "missing character"
problem as well.

Anyway, good luck. For what it's worth, this C# newsgroup may not be the
best place to look for help with this question. It's pretty remotely
related to the main topic of the newsgroup. If you can find a forum that
is more specific to automating Office applications, or even doing
..NET-to-Office interop specifically, you'd probably find people who have a
lot more experience and can offer more specific advice.

Pete
 
M

mrstrong

By the way, with respect to the suggestion from Kalpesh, I'm wondering if
there's an RTF format you can use with the BodyFormat property instead of
"olFormatHTML" (maybe it's called "olFormatRTF" :) ). Then you could set
the Body property to the RTF you get directly from the RichTextBox rather
than going through Word. Less conversion is always better and maybe that
would address the issues you're running into, assuming such a format is
supported. Who knows? It might even avoid the "missing character"
problem as well.

Thanks that is what I tried initially but to no avail. The actual rtf
code is displayed, rather than the actual formatted rtf.

Anyway, good luck. For what it's worth, this C# newsgroup may not be the
best place to look for help with this question. It's pretty remotely
related to the main topic of the newsgroup. If you can find a forum that
is more specific to automating Office applications, or even doing
.NET-to-Office interop specifically, you'd probably find people who have a
lot more experience and can offer more specific advice.

Sure, I'll go looking.

If I find a way forward I will let you know.

Regards,

Peter
 
M

mrstrong

Thanks that is what I tried initially but to no avail. The actual rtf
code is displayed, rather than the actual formatted rtf.


Sure, I'll go looking.

If I find a way forward I will let you know.

OK - my extravagant solution is this:

Clipboard.Clear();
richTxtBox.SelectAll();
richTxtBox.Copy(); // copy contents of the
rich text box to clipboard...
msg.Show(); // open the outlook message
SendKeys.Send("^(v)"); // paste from the
clipboard to the email message in Outlook.

Good old sendkeys :)

Hopefully this helps someone out there.

Peter
 
M

mrstrong

OK - my extravagant solution is this:

Clipboard.Clear();
richTxtBox.SelectAll();
richTxtBox.Copy(); // copy contents of the
rich text box to clipboard...
msg.Show(); // open the outlook message
SendKeys.Send("^(v)"); // paste from the
clipboard to the email message in Outlook.

After further testing, I added in a Sleep(50); just to give Outlook a
bit of time to open up before the paste was done. Without this about
15% of the time it would not paste correctly, presumably because the
Outlook message was not properly open yet before the paste was called.

msg.Show();
System.Threading.Thread.Sleep(50);
SendKeys.SendWait("^(v)");


Regards,

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top