I'm trying to create an Outlook add-in that processes a certain MailItem and forwards the result on to another group of people. In order to do the processing, I believe I need to use the WordEditor in order to get the proper formatting, but when I try to access that object, it returns null. I'm able to send the mail properly when I don't access the WordEditor, so it's probably something simple I've overlooked. How can I access the WordEditor for this new MailItem?
Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder inbox;
Outlook.Items items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
outlookNameSpace = this.Application.GetNamespace("MAPI");
inbox = outlookNameSpace.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.
OlDefaultFolders.olFolderInbox);
items = inbox.Items;
items.ItemAdd +=
new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}
void items_ItemAdd(object Item)
{
string filter = "Filter";
if (Item is Outlook.MailItem)
{
Outlook.MailItem mail = (Outlook.MailItem)Item;
if (Item != null)
{
if (mail.MessageClass == "IPM.Note" &&
mail.Subject.ToUpper().Contains(filter.ToUpper())
&& mail.Attachments.Count == 1)
{
Outlook.Attachments attachments = mail.Attachments;
if (attachments != null)
{
Outlook.Attachment file = attachments[1];
//Process attachment...
Outlook.MailItem newMail = this.Application.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector insp = newMail.GetInspector;
insp.Activate();
newMail.Subject = "Subject";
newMail.To = "[email protected]";
Word.Document emailBody = insp.WordEditor as Word.Document;
//emailBody is always null
newMail.Send();
System.Runtime.InteropServices.Marshal.ReleaseComObject(newMail);
System.Runtime.InteropServices.Marshal.ReleaseComObject(file);
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(attachments);
}
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(mail);
}
}