Li Pang,
That suggests to me that the first item is not a MailItem, it may be a
ReportItem, MeetingItem, one of the TaskRequestItems, or some other COM
object.
If you use TypeOf what kind of object does it match?
Dim item As Object = oItems.GetFirst()
If TypeOf item Is Outlook.MailItem Then
' we got a mail item
ElseIf TypeOf item Is Outlook.ReportItem Then
' we got a report item
ElseIf TypeOf item Is Outlook.MeetingItem Then
' we got a meeting item
Else If TypeOf ... for all the other Outlook Item types.
' we got another outlook item type...
End If...
Hope this helps
Jay
| Jay,
|
| Seems it is an Outlook issue. I use Option Strict On and use outlook to
| catch an mailitem, no error at compiling time but got a runtime error at
| below line:
|
| oMailItem = CType(oItems.GetFirst(), Outlook.MailItem)
|
| "Specified cast is not valid."
|
|
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > Li,
| > Yes:
| >
| >
http://msdn.microsoft.com/library/d...n-us/cdo/html/_olemsg_id_property_message.asp
| >
| > Are you getting a compile error or a runtime error? My sample addresses
| > compile errors (caused by Option Strict On).
| >
| > If you are getting a runtime error, can you show the entire exception
| > (Exception.ToString) to better identify the problem.
| >
| > Hope this helps
| > Jay
| >
| > | > | Jay,
| > |
| > | Thank for your help. But I still got the same error. Are you sure the
ID
| > is
| > | a string?
| > |
| > | "Jay B. Harlow [MVP - Outlook]" wrote:
| > |
| > | > Li,
| > | > Do you have Option Strict On?
| > | >
| > | > If you have Option Strict On:
| > | > - cdoMsg.ID has a return type of Object (which contains a String)
| > | > - Session.GetItemFromID also has a return type of Object (which
based on
| > the
| > | > ID, may contain a MailItem)
| > | >
| > | >
| > | > Try something like:
| > | >
| > | > Dim outlookApp As Microsoft.Office.Interop.Outlook.Application
| > | >
| > | > | Dim outlookMsg AsMicrosoft.Office.Interop.Outlook.MailItem
| > | > | Dim cdoMsgAs MAPI.Message
| > | >
| > | > | outlookMsg = DirectCast( _
| > | > outlookApp .Session.GetItemFromID( _
| > | > DirectCast(cdoMsg.ID, String) _
| > | > ), Outlook.MailItem)
| > | >
| > | > Which tells the compile that cdoMsg.ID returns a String, and
| > | > Session.GetItemFromID returns an Outlook.MailItem.
| > | >
| > | > Hope this helps
| > | > Jay
| > | >
| > | >
| > | > | > | > | Hi,
| > | > |
| > | > | I used following codes to pass a message item from CDO to Outlook.
| > They
| > | > | worked fine when I used outlook 2000, but get an error of
"Specified
| > cast
| > | > is
| > | > | not valid." when I used Outlook 2003. Anybody has an ideal?
| > | > |
| > | > | Dim outlookMsg AsMicrosoft.Office.Interop.Outlook.MailItem
| > | > | Dim adoMsg As MAPI.Message
| > | > | outlookMsg =
| > | > |
| > | >
| >
Microsoft.Office.Interop.Outlook.Application.Session.GetItemFromID(cdoMsg.ID)
| > | > |
| > | > |
| > | >
| > | >
| > | >
| >
| >
| >