简体   繁体   English

VB.net中的Outlook错误

[英]Outlook Error in VB.net

The below code was working perfectly in last week and now am getting this error I don't know where it went wrong. 下面的代码在上周运行良好,现在出现此错误,我不知道哪里出错了。

unable to. 无法。 cast com object of type 'system.__comobject' to interface type 'Microsoft.office.interop.outlook.mailitem 将类型为'system .__ comobject'的com对象转换为接口类型为'Microsoft.office.interop.outlook.mailitem

Try
    Dim olApp As Outlook.Application
    Dim olNs As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    Dim i As Integer

    olApp = New Outlook.Application
    olNs = olApp.GetNamespace("MAPI")

    Dim Fldr As MAPIFolder
    Fldr = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) 
    i = 1

    For Each olMail In Fldr.Items
        If InStr(olMail.Subject, TextBox1.Text) <> 0 Then
            olMail.Display()
            i = i + 1
        End If
    Next olMail

    Catch ex As System.Exception
        MsgBox(Err.Description)
End Try

Probably there is/are objects other than MailItem (eg ReportItem, MeetingItem) in the items list. 项目列表中可能存在MailItem以外的其他对象(例如ReportItem,MeetingItem)。 Try below 试试下面

Try
    Dim olApp As Outlook.Application
    Dim olNs As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    Dim oObject As Object
    Dim i As Integer

    olApp = New Outlook.Application
    olNs = olApp.GetNamespace("MAPI")

    Dim Fldr As MAPIFolder
    Fldr = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) 
    i = 1

    For Each oObject In Fldr.Items
        If TypeOf oObject Is Outlook.MailItem Then
            olMail = CType(oObject, Outlook.MailItem)  
            If InStr(olMail.Subject, TextBox1.Text) <> 0 Then
                olMail.Display()
                i = i + 1
            End If
        End If
    Next olMail

    Catch ex As System.Exception
        MsgBox(Err.Description)
End Try

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM