Automating Outlook "New Mail Message" from .NET Winforms - Upgrade from VS2003 to 2005 breaks

C

ChrisA

I have a WinForms app, written in VB.NET, with VS 2003, which opens an
Outlook 2003 (or 2002) "new message" form with the following code:

------------------------------------------------
' lines doing setup snipped

Me.WordDoc = Me.WordApp.Documents.Add(' blah blah

Me.OutgoingMailItem = CType(WordDoc.MailEnvelope.Item,
Outlook.MailItemClass)

' lines setting message properties snipped

AddHandler OutgoingMailItem.ItemEvents_Event_Send, AddressOf Me.HandleSend
AddHandler OutgoingMailItem.ItemEvents_Event_Close, AddressOf
Me.HandleWordAppClose
Me.WordApp.Visible = True

-------------------------------------------------

This works fine with 1.1 and VS 2003. I can catch the "Send" event and the
message ends up in the Sent Items folder.

However, having imported the whole solution to VS 2005, I get a run time
error on

Me.OutgoingMailItem = CType(WordDoc.MailEnvelope.Item,
Outlook.MailItemClass)

The text of the error message is:

"Unable to cast COM object of type 'System.__ComObject' to class type
'Outlook.MailItemClass'. 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."

If instead I cast the OutgoingMailItem to the "Outlook.MailItem" interface
type (instead of MailItemClass), the ItemEvents_Event_Send and
ItemEvents_Event_Close events seem not
to be available, so I can't catch the send (or the close - I need both).

Any suggestions or workarounds gratefully received.

ChrisA
 
R

rkozlin

Does no one have a solution for this??? Hello???? Microsoft where are you?

I am having the exact same problem while attempting to get the Item.Send
event from the mailitem on an open inspector in Outlook. You have to use the
MailItemClass in order to get a lot of the MailItem events. So the issue is
in attempting to instantiate the MailItemClass.

In looking at how we do this with the InspectorClass - casting from
Inspector - one would expect they could get a MailItemClass by casting from
MailItem. In doing so you get the issue ChrisA posted below.

ChrisA the reason you are getting the error is because
'WordDoc.MailEnvelope.Item' is of type Outlook.MailItem not
Outlook.MailItemClass. I know, this does not help you achieve what you want
to do.

Please, if you have experienced this issue as well (read the error that
ChrisA posted below) reply here as well.

Please do not reply a 'possible' solution if you know you really do not know
what you are talking about. We need someone who knows their stuff here
answering this please!


~Rick
 

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