[英]System.InvalidCastException HResult=0x80004002 Message=Unable to cast COM
I am trying to read the sent items folder of my outlook, with the following code:我正在尝试使用以下代码读取我的 outlook 的已发送项目文件夹:
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Outlook
Dim moApp As Outlook.Application
Dim moNS As Outlook.NameSpace
moApp = New Outlook.Application
moNS = moApp.GetNamespace("MAPI")
Dim oFolder As Outlook.MAPIFolder
Dim oEmail As Outlook.MailItem
oFolder = moNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
For Each i As Object In oFolder.Items
If TypeOf i Is MailItem Then
MessageBox.Show("From: " & i.To)
End If
Next
outlook folders outlook 个文件夹
when I try to read the information of the MailItem object I get the error当我尝试读取 MailItem object 的信息时出现错误
"System.InvalidCastException
HResult=0x80004002
Message=Could not convert COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-0000000000000046}' failed due to the following error: No such interface is supported (0x80004002 (E_NOINTERFACE)).
Source=SendMail"
But when I try it with the inbox folder, it works without any problem.但是当我尝试使用收件箱文件夹时,它没有任何问题。
I am also validated to receive a MailItem
object.我还经过验证可以收到MailItem
object。
Has this error happened to anyone else?这个错误发生在其他人身上吗?
Try to explore the problematic item in any low-level property explorer tool such as MFCMAPI or OutlookSpy.尝试在任何低级属性浏览器工具(如 MFCMAPI 或 OutlookSpy)中探索有问题的项目。 Is there any difference between other items in the folder?文件夹中的其他项目有什么区别吗? Is it a true mail item?它是真正的邮件吗?
You may also try to use the late binding technology represented in.Net by reflection.你也可以通过反射尝试使用.Net中代表的后期绑定技术。 The Type.InvokeMember allows to get property value without casting objects. Type.InvokeMember允许在不转换对象的情况下获取属性值。
Note, you can use the "is" and "as" operators in C#. See How to: Programmatically Determine the Current Outlook Item for more information.请注意,您可以在 C# 中使用“is”和“as”运算符。有关详细信息,请参见如何:以编程方式确定当前 Outlook 项目。 If the is
operator throws an exception you may try to use the as
instead.如果is
运算符抛出异常,您可以尝试使用as
代替。
Also the Outlook object model provides the MessageClass property - a string representing the message class for the Outlook item. Outlook object model 还提供MessageClass属性 - 表示 Outlook 项目的消息 class 的字符串。 Under the hood the message class is used to identify what inspector to use in Outlook for displaying the item.在引擎盖下,消息 class 用于标识在 Outlook 中使用哪个检查器来显示项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.