How to prepopulate an Outlook MailItem and avoiding a com Exception from the object model guard

1

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

c#
windows-7
outlook
integration
asked on Stack Overflow May 11, 2010 by torrente • edited May 11, 2010 by torrente

2 Answers

2

Take a look at Dimitry's Redemption Lib, It was designed to do exacly this.

answered on Stack Overflow May 11, 2010 by 76mel
0

Well,

I've already spent way too much time and energy on this question so I think I came up with a pragmatic workaround for my particular case - but no real solution!

The problem is apparently due to the programms running at different integrity levels (Outlook = medium, VS2010 = admin or high). Office runs by default on a medium level and so will my future application once deployed. So there shouldn't be any trouble, since if the CRM and Outlook run at the same level, there's no problem.

For development I just let em both run on high, or medium (starting my compiled files from the debug folder).

In any other case a Messagebox warning is shown informing the user of the different integrity levels that cause an exception and prevent access.

At the code level, when I try to read any (by Outlook) prepolutated, protected properties and the object model guard raises the exception, I just catch it and use default values instead.

Why I had to read them in first place is currently beyond me - but so were the specs which were handed to me.

Anyway thanks for reading and if I ever come up with a solution I'll be sure to psot it - until then keep in mind that a pragmatic solution is better then none!

Happy Father's Day everyone!

answered on Stack Overflow May 12, 2010 by torrente

User contributions licensed under CC BY-SA 3.0