Outlook Addin Trust

M

Mark

Ive created an Outlook addin using VSTO 2005, that basically creates an email
using receipients you have selected from a list.

However, whenever it displays the email, the object model guard pops up and
asks me whether i want to allow it or not.

Ive setup the trusted COM thing through Exchange using the Outlook Security
Settings public folder, but now that ive trusted it, i get the following error

"Unable to cast object of type 'System.__ComObject' to type
'Microsoft.Office.Interop.Outlook.ApplicationClass'"

Anyone have any suggestions?
 
T

Thaddaeus Parker

"Unable to cast object of type 'System.__ComObject' to type
'Microsoft.Office.Interop.Outlook.ApplicationClass'"
This happens when you are passing a pointer into a function and then
attempting to cast it as something else. Case in point:
TaskItem taskItem = application.CreateItem(OlTaskItem);
BuildItem(taskItem);

public void BuildItem(TaskItem tItem){
TaskItemClass taskClass = (TaskItemClass)tItem; <-- an InvalidCastException
will be thrown here.
}
In the object browser you should note that any Outlook object that has a
Class suffix is really not intended to be used in Managed Code. What you
need to do is pass the item as an object into your function or ctor and then
cast the object as whatever Outlook Object you want.
The Outlook Object model doesn't have a readily apparent distinction between
what an interface and a class is. With a COM object it is easier to cast
with an interface because of CCW/RCW IUknown leeway. The above code block
revamped would look like this:
object taskItem = application.CreateItem(OlTaskItem);
BuildItem(taskItem);

public void BuildItem(object tItem){
TaskItem taskClass = (TaskItem)tItem; <-- no exception is thrown and you
can do with the item however you wish.
}

Regards,

Thaddaeus.
 
M

Mark

The code where it gets a cast exception is this line:

Outlook.Application objOutlook = new Outlook.Application();

I cant see how this is casting anything out of the ordinary and why it would
throw an exception, any ideas?
 
T

Thaddaeus Parker

If you are creating a new Application object instead of using the one
application object that is a part of the the add-in , you will have security
issues. This is especially true when you go and create a new item with the
new application object ( not the one that is apart of the add-in) and
attempt to add or manipulate it with your add-in's folders and such. If you
have a wrapper class that manipulates the item, ensure that you pass in the
Connect application object so that you can create a new item with no issues.
Also be aware that if you try to create new instance of an Outlook
application object from within your add-in (which already contains an
application object) the code with throw an unhandled exception.

Without knowing what your code is actually trying to do, I can't give many
more reasons for the exception.
Regards,
Thaddaeus

Thaddaeus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top