Pass Object From CDO to Outlook

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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)
 
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)
|
|
 
Jay,

Thank for your help. But I still got the same error. Are you sure the ID is
a string?
 
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)
| > |
| > |
| >
| >
| >
 
Jay,

I didn't use Option Strict On because it causes other validation. I have
only one single problem to pass message ID from CDO to Outlook.
the id is a long string like
0000000043A5913410C2B848A9ECF84FFE693BF40700C8C0B8F6F0E3D211BAC00008C7C9AA8C00001E9052E900000030C812DEF7FC4CBDA6B42C363192E300000027962A0000

and exception after the line

Try
oMsg =
Outlook.Session.GetItemFromID("0000000043A5913410C2B848A9ECF84FFE693BF40700C8C0B8F6F0E3D211BAC00008C7C9AA8C00001E9052E900000030C812DEF7FC4CBDA6B42C363192E300000027962B0000")
Debug.WriteLine("OK")
Catch ex As System.Exception
Debug.WriteLine("ERROR: " & ex.Message)
End Try

is "Specified cast is not valid"

This error persists by using DirectCast in your codes.



Jay B. Harlow said:
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)
| > |
| > |
| >
| >
| >
 
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."
 
Li Pang,
As I requested: You need to include *all* the text from Exception.ToString
not ex.Message, you need:

| Catch ex As System.Exception
| Debug.WriteLine("ERROR: " & ex.ToString())
| End Try

Cut & paste all the lines that the Debug.WriteLine produces...

This will better help identify where specifically the problem is.

Hope this helps
Jay



| Jay,
|
| I didn't use Option Strict On because it causes other validation. I have
| only one single problem to pass message ID from CDO to Outlook.
| the id is a long string like
|
0000000043A5913410C2B848A9ECF84FFE693BF40700C8C0B8F6F0E3D211BAC00008C7C9AA8C00001E9052E900000030C812DEF7FC4CBDA6B42C363192E300000027962A0000
|
| and exception after the line
|
| Try
| oMsg =
|
Outlook.Session.GetItemFromID("0000000043A5913410C2B848A9ECF84FFE693BF40700C8C0B8F6F0E3D211BAC00008C7C9AA8C00001E9052E900000030C812DEF7FC4CBDA6B42C363192E300000027962B0000")
| Debug.WriteLine("OK")
| Catch ex As System.Exception
| Debug.WriteLine("ERROR: " & ex.Message)
| End Try
|
| is "Specified cast is not valid"
|
| This error persists by using DirectCast in your codes.
|
|
|
| "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)
| > | > |
| > | > |
| > | >
| > | >
| > | >
| >
| >
| >
 
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)
| > | > |
| > | > |
| > | >
| > | >
| > | >
| >
| >
| >
 
Sorry Jay,

I think I know the reason but still don't have a solution.

The error occured when to catch a message with the digital signature. But
the same codes can read successfully any message without the digital
signature. I think this is due to Outlook 2003 protection.

With Outlook 2000 the codes run without the error.

All I need is to read a message with digital signature, and move the
attachment to a user' defined location.

Possible solutions:
1. Read messages through Outloo 2003
2. Read messages through CDO, but how to get the attachment and save it into
a specific location?



Jay B. Harlow said:
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)
| > | > |
| > | > |
| > | >
| > | >
| > | >
| >
| >
| >
 

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

Back
Top