How to prepopulate an Outlook MailItem and avoiding a com Exception from the object model guard
- by torrente
Hi,
I work for a company that develops a CRM tool and offers integration with MS Office(2003 & 2007) from windows XP to 7. (I'm working using Win7)
My task is to call an Outlook instance (using C#) from this CRM tool when the user wants to send an email and prepopulate with data of the CRM tool (email, recipient, etc..)
All of this already works just fine.
The problem I'm having is that Outlook's "object model guard" is throwing com Exception
(Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)))
the moment I try to read a protected value from the mailItem (such as mail.bodyHTML).
Example Snippet:
using MSOutlook = Microsoft.Office.Interop.Outlook;
//untrusted Instance
_outlook = new MSOutlook.Application();
MSOutlook.MailItem mail = (MSOutlook.MailItem)_outlook.CreateItem(MSOutlook.OlItemType.olMailItem);
//this where the Exception occurs
string outlookStdHTMLBody = mail.HTMLBody;
I've done quite a bit of reading and know that my Outlook Instance (derived by using new Application) is considered untrusted and therefore the "omg" kicks in.
I do have a workaround for development:
I'm running VS2010 as Administrator and if I run Outlook as Administrator as well - all is good. I suppose this is due to them having the same integrity levels(high) and the UAC(?) is not complaining. But that just ain't the way to go for deployment.
Now the question is:
Is there a way to obtain a trusted instance of Outlook so that I can avoid this exception?
I've already read that when developing an Office Add-In using VSTO one can obtain a trusted Instance from the OnComplete event and/or using "ThisAddin"
But I "merely" want to start an outlook instance and preopulate it, and do not want to develop an Add-In since this is not the requirement.
And to make it clear - I have no problem with pop ups informing the user that outlook is being accessed - I just want to get rid of the exception!
So how can I get around this problem using code?
Any help is highly appreciated!
Thomas