System.InvalidCastException Startup Problem

Add-in Express™ Support Service
That's what is more important than anything else

System.InvalidCastException Startup Problem
 
David Ing




Posts: 56
Joined: 2006-06-27
I have a few customers using my Outlook addin based on the new ADX2.7 and OLExt 1.3. Most are doing fine.

The problem I have is that one customer has an add-in startup issue. The error in the ADX exception dialog is:

AddinModule.AddinModule_AddinStartupComplete
Exception Type: System.InvalidCastException
Exception Message: Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.Office.Interop.Outlook.ExplorersClass'...

Has anyone seen this before?

It happens when I try to add the new Explorer instance to my wrapper collection.

I've tried to make sure the correct Outlook PIA's are referenced etc. Others with the same install are doing ok.

Anyone got any clues? I'm running out of things to try and he was fine before I started using ADX with my addin...
Posted 08 Aug, 2006 05:58:54 Top
Matt Driver


Matt


Posts: 152
Joined: 2004-08-17
Hi

Can you post the code here and I will have a look. Sounds like the object you are casting is either null which I have had occur if the explorer closes or is not an explorer object.

I use:

Outlook.Selection selItem null;
Outlook.Explorer activeExplorer =null;
activeExplorer = OutlookApp.ActiveExplorer;
try
{
selItem = activeExplorer.Selection;
}
catch
{
Marshall.ReleaseCOMObject(selItem);
Marshall.ReleaseCOMObject(ActiveExplorer);
}

I use the try catch as sometimes I have found outlook closes an explorer and this way I catch it without a exception.


Matt
Posted 08 Aug, 2006 07:26:00 Top
David Ing




Posts: 56
Joined: 2006-06-27
Thanks Matt - It would be hard to strip down the code (there is a lot), and I was kind of hoping it would be an environment/system thing, rather than the code, purely as it works on so many other peoples install with the same addin installation.

I'm having trouble recreating it on my test systems (i.e. clean images etc), and the only clue so far was that I saw it with a Outlook 2007 Beta 2 setup. I'll keep going :-)
Posted 08 Aug, 2006 07:57:09 Top
Matt Driver


Matt


Posts: 152
Joined: 2004-08-17
I don't think OL2007 is fully supported yet its planned for Q4 I believe so if you are getting odd things only happening on Ol2007 I would not be suprised.

Matt
Posted 08 Aug, 2006 08:06:49 Top
David Ing




Posts: 56
Joined: 2006-06-27
Yep, but the customer with the error is on Outlook 2003 (I've verified this).

Just saying that OL2007B2 was the only other place I had seen it happen.
Posted 08 Aug, 2006 08:14:24 Top
Matt Driver


Matt


Posts: 152
Joined: 2004-08-17
does it happen everytime they start the add-in or just a random occurence
Posted 08 Aug, 2006 08:54:16 Top
David Ing




Posts: 56
Joined: 2006-06-27
Every time (for that customer)...

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.Office.Interop.Outlook.ExplorerClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject type. Instances of this type cannot be cast to any other class; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
Posted 08 Aug, 2006 09:43:01 Top
Matt Driver


Matt


Posts: 152
Joined: 2004-08-17
Apparently this can be due to framework 2.0 being installed on a machine whilst loading a 1.1 add-in. In the ADX project under Host Configuration do you have Outlook and any other apps supported by the addin set to Auto, 1.1 or 2.0 ?

I would set this to which ever framework you are using rather than to auto rebuild and see if that installs.

Another thing I have found out the hard way, if you look at:


Microsoft.Office.Interop.Outlook.MailItemClass sendMial = (Microsoft.Office.Interop.Outlook.MailItemClass)outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

this works under 1.1 but when i changed frame work 2.0 it fails due to the first line part "Microsoft.Office.Interop.Outlook.MailItemClass" where the type is of MailItemClass

if this is updated to:

Microsoft.Office.Interop.Outlook._MailItem sendMial = (Microsoft.Office.Interop.Outlook.MailItem)outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

I.E. _MailItem the object rather than class it works under 2.0.

Check your code to ensure you are creating objects of the type not class.

Matt
Posted 08 Aug, 2006 10:04:42 Top
David Ing




Posts: 56
Joined: 2006-06-27
Thanks for your help so far Matt.

Where does the Host Configuration for framework info get stored (I wanted to see under the Designer) and how does it get onto the installed addin?

Also, the reason I was using ExplorerClass rather than Explorer was that it has the ExplorerEvents_Event_Close event - which I need to fire my CloseEventHandler. How do you trap the Explorer closing otherwise?

It's good to know that it's a runtime related thing though - worse case is that I could move all my users up to 2.0 (or provide a seperate download for both, which I've avoided so far).
Posted 08 Aug, 2006 10:37:00 Top
Matt Driver


Matt


Posts: 152
Joined: 2004-08-17
Hi

I believe the Host Config info is a property of the actuall addin supplied by ADX and is used in the primary output of the add-in when it is build as their are no manifest files generated as part of ADX. I do not believe their are any files with this info stored in them apart from the VS project itself. I believe it is it the shim or loader than uses this propery to load the addin correctly to run under the framework specified.

To detect a Explorer Close use the events provided by ADX. If you have not already done so in the AddinModule properties add item adxOutlookEvents. Select the icon that appears on the screen and then in its properties selct the yellow event icon and scroll down to "Explorer Close" then double click this will add the event to the project for you to add code to.

Matt
Posted 08 Aug, 2006 10:58:38 Top