exception in setting Outlook.MailItem.body as rich text
Asked Answered
I

2

5

I have a RichTextBox for the Message Body and I need to create a new mail with the body of rich Text. There is my code how i tried to realize that:

MemoryStream ms = new MemoryStream();
MemoEditBody.SaveDocument(ms, DevExpress.XtraRichEdit.DocumentFormat.Rtf);
byte[] RTFBody = ms.ToArray();
email.oMsg.RTFBody = RTFBody;                                

Types:

OutlookEMail email;
public MailItem oMsg;

Problem: Working with 2010 Outlook - works properly; Working with 2007 Outlook -this code throws exception (AccessViolationException: Attempted to read or write protected memory) Can someone suggest how to resolve this problem with 2007 Outlook?

P.S. Using : Visual studio 2010 DevExpress

It was tested on two computers, they are almost identical (same windows, same framework, only one has Outlook 2007 another 2010) so it is 80% that problem is in Outlook version.

Inconvincible answered 12/7, 2012 at 8:23 Comment(0)
F
4

I'm not sure if you managed to solve your problem but I was stuck with the same thing. I was using the DevExpress RichEditControl to create a mail merged RTF file which would be used as the RtfBody of an Outlook MailItem. However, I was getting the same AccessViolationException.

My workaround, which is not the most elegant, was to convert the RTF to HTML using this method:

http://www.codeproject.com/Articles/51879/Converting-RTF-to-HTML-in-VB-NET-the-Easy-Way

Then just set the message .BodyFormat to olFormatHTML and set the .HTMLBody to the string returned from the function above.

Fruitage answered 1/11, 2012 at 8:4 Comment(1)
Do you have any idea about the embbeded images?? All paths that get inside html text are local so they are not shown.Kalakalaazar
B
3

The reason is that the MailItem.RTFBody property was only added in Outlook 2010, so you were attempting to call something that doesn't exist.

There are two potential workarounds as far as I can see.

  1. If Outlook is using Word as the editor, you can extract the message body using:

    Word.Document doc = app.ActiveInspector.WordEditor as Word.Document;
    

    ... then you can use almost any of the Word object model to edit/extract the text (eg doc.SaveAs(...) to save it).

    Note that the WordEditor property is only valid if the IsWordMail method returns true and the EditorType property is olEditorWord (see http://msdn.microsoft.com/en-us/library/office/ff868196.aspx).

  2. You can convert the body to HTML first as Foub says above, but that may lose formatting.

Neither solution is perfect, but in my experience a mix of the above works most times.

Bramlett answered 26/11, 2013 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.