views:

148

answers:

2

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

A: 

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

76mel
Hi mel - thx for the quick reply!I already considered the Redemption Lib but unfortunately it's not for free with commercial use.--------------------------------------------"The version of Redemption available from the link below is licensed for the development purposes only. It cannot be used with any commercial software.If you plan to include Redemption with a commercial product, you must purchase a distributable version which you can then use for any and all of your products."
torrente
Simple Mapi may work for you as well its how older office apps work.Also MAPI33.dll is still kicking around on the net in places http://stackoverflow.com/questions/362359/download-location-for-mapi33-dll I also think there are registry hacks to trun it off as well but they do depend on the version of outlook being used.
76mel
A: 

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!

torrente