【问题标题】:VBA Excel - Outlook - For Each - Returns mail = nothing - Error mngtVBA Excel - Outlook - 对于每个 - 返回邮件 = 无 - 错误管理
【发布时间】:2017-04-08 20:51:37
【问题描述】:

我在 Excel 上做了一个小宏,旨在在 Outlook 上查找具有特定主题和特定接收时间的邮件。几乎所有人都能很好地工作,但我的一位同事的 oulook 收件箱中出现了错误。

我写的代码:

For Each olMail In oDefaultFolder.Items
    If olMail.Subject = "DDJ of " & date_target Then
        MyAr = Split(olMail.Body, vbCrLf)

        For i = LBound(MyAr) To UBound(MyAr)
        Sheets("DDJ").Cells(i + 1, 1).Value = MyAr(i)
        Next i
        ddj_empty = False
        Exit For
    End If
Next olMail

我尝试了 3 个 Outlook 帐户,但没有 pb,但第 4 个帐户出错了。我手动删除了一个,但太多了,从约会 conf 到看起来很正常的邮件

它返回的 pb 突出显示“下一个 oLMail”并显示 oLMail = Nothing 我希望我的宏继续并继续搜索,但我在处理这个没有任何错误时遇到了一些麻烦。

我正在寻找类似于“如果邮件 = 没有然后 i = i+1 .. 下一个 i”的 smtg,但我在谷歌上找到了一个“继续”,但我的 excel 无法识别继续功能?所以我不知道该怎么办

有什么建议吗? 非常感谢

【问题讨论】:

    标签: vba email foreach error-handling outlook


    【解决方案1】:

    Outlook 文件夹可以包含different item types。不同的类型并不相同。例如,MeetingItem 支持MeetingWorkspace propertyMailItems不要。

    在使用olMail 之前,最好使用vba TypeName 函数检查类型。

    ' Perform action on specific item types only.
    ' TESTED: Outlook 2010.
    Sub CheckItemType()
        Dim i As Object         ' Used to loop over items.
        Dim iType As String     ' Used to hold type of above item.
    
            ' Loop over all items in the inbox.
            For Each i In Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
                iType = TypeName(i)
    
                ' Limit actions to required types.
                If iType = "MailItem" Or iType = "MeetingItem" Then
                    ' Your logic.
                End If
            Next
    End Sub
    

    【讨论】:

    • 对不起!我使用 Outlook 编写了代码。刚刚注意到您的示例取自 Excel。 TypeName 和 If 语句是您需要的唯一位。只需将这些添加到您现有的代码中。
    • 谢谢,我会试试看,如果它修复了 pb,我会告诉你!
    猜你喜欢
    • 2013-05-27
    • 1970-01-01
    • 2014-03-21
    • 2015-08-27
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    相关资源
    最近更新 更多