【问题标题】:How to bypass Outlook item that generates an error when replying to mail using Excel VBA?如何绕过使用 Excel VBA 回复邮件时生成错误的 Outlook 项目?
【发布时间】:2018-08-07 20:50:42
【问题描述】:

我有工作代码可以根据主题回复用户 Outlook 中的电子邮件。如果最近的项目是会议邀请,我的代码将无法检索我想要的电子邮件。相反,它不会通过会议邀请并显示错误。

代码如下。

Sub Display()
Dim Fldr As Outlook.Folder
Dim olfolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem
Dim olReply As Outlook.MailItem
Dim olItems As Outlook.Items
Dim i As Integer
Dim signature As String


Set Fldr = Session.GetDefaultFolder(olFolderInbox)
Set olItems = Fldr.Items
olItems.Sort "[Received]", True
For i = 1 To olItems.Count
    signature = Environ("appdata") & "\Microsoft\Signatures\"
    If Dir(signature, vbDirectory) <> vbNullString Then
        signature = signature & Dir$(signature & "*.htm")
    Else:
        signature = ""
    End If
    signature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll

    Set olMail = olItems(i)
    If InStr(olMail.Subject, Worksheets("Checklist Form").Range("B8")) <> 0 Then
        If Not olMail.Categories = "Executed" Then
            Set olReply = olMail.ReplyAll
            With olReply
             .HTMLBody = "<p style='font-family:calibri;font-size:14.5'>" & "Hi Everyone," & "<p style='font-family:calibri;font-size:14.5'>" & "Workflow ID:" & " " & Worksheets("Checklist Form").Range("B6") & "<p style='font-family:calibri;font-size:14.5'>" & Worksheets("Checklist Form").Range("B11") & "<p style='font-family:calibri;font-size:14.5'>" & "Regards," & "</p><br>" & signature & .HTMLBody

             .Display
             .Subject = "RO Finalized WF:" & Worksheets("Checklist Form").Range("B6") & " " & Worksheets("Checklist Form").Range("B2") & " -" & Worksheets("Fulfillment Checklist").Range("B3")
            End With

            Exit For
        olMail.Categories = "Executed"
        End If
    End If
Next i

End Sub

如果代码无法通过第一封电子邮件,是否可以绕过最近的项目。示例:会议邀请

【问题讨论】:

  • 我认为错误是Set olMail = olItems(i)上的类型不匹配
  • 我怀疑相同但不同的签名问题可能与您/他们的帐户如何配置回复签名有关。
  • @MathieuGuindon 你能帮我解决这个问题吗?真的很感激。 stackoverflow.com/questions/51770455/…

标签: vba excel outlook


【解决方案1】:
Dim olMail As Outlook.MailItem
...
Set olMail = olItems(i)

Set 分配不仅会在第一个项目是会议邀请时失败,而且对于不是 Outlook.MailItem 实例的任何 olItems(i)(即 i 的任何值)也会失败。这包括任何可能进入 Outlook 收件箱的内容,包括会议邀请。

一种方法是处理olItems(i) 不是MailItem 的特定情况下引发的运行时错误:

    For i = 1 To olItems.Count
        On Error GoTo ErrHandler ' jumps to error-handling subroutine if there's an error
        Set olMail = olItems(i)
        On Error GoTo 0 ' let any other error blow everything up
        ...
SkipToNext:
    Next i
    Exit Sub
ErrHandler:
    Debug.Print "Item index " & i & " is not a MailItem; skipping."
    Resume SkipToNext

请注意,我会尽早将分配/验证放入循环中 - 这样,如果您没有查看 MailItem,就不会运行无用的指令。


另一种更好的方法是验证olItems(i)的类型

Dim olItem As Object
'...

    For i = 1 To olItems.Count
        Set olItem = olItems(i)
        If Not TypeOf olItem Is Outlook.MailItem Then Goto SkipToNext
        Set olMail = olItem ' essentially a type cast from Object to MailItem
        ...
    SkipToNext:
    Next

或者,您可以放弃 GoTo 跳转并增加嵌套级别:

    For i = 1 To olItems.Count
        Set olItem = olItems(i)
        If TypeOf olItem Is Outlook.MailItem Then
            Set olMail = olItem ' essentially a type cast from Object to MailItem
            ...
        End If
    Next

注意缩进;如果您不确定如何正确且一致地执行此操作,请随时使用indenter。适当的缩进对于代码的可读性至关重要,尤其是考虑到嵌套循环和条件结构(免责声明:我拥有该网站和它所针对的 OSS 项目)。

【讨论】:

  • 谢谢 Mathieu,我使用了第二种解决方案,效果很好!非常感谢您在此答案中投入的时间和细节。关于签名,所有用户使用相同的程序和大致相同的签名,我会深入研究。
  • 如果您愿意,我可以提出一个新问题,但我会将它放在这里以防简单修复。截至目前,我的电子邮件只会回复用户特定的收件箱。这在大多数情况下都很好,除了少数用户还需要从其他常规收件箱中获取代码。我将如何更改我的代码以允许从任何收件箱中捕获或使用具有正确收件箱名称的 worksheets("sheet1").Range("A1") 回复。谢谢
  • 我在第二个解决方案中发现了一个错误。 For I = 1 to olItems.Count 的代码行为一位收件箱中有超过 60,000 封电子邮件的用户显示错误 6“溢出”。使用它的其他人收到的电子邮件要少得多。我需要在此代码中添加一些内容以超越此错误吗?在此先感谢,非常感谢!
  • @Tmacjoshua 你的错误。 Integer 是一个 16 位有符号整数类型,最大值为 2^15-1,即 32,768 溢出它。除了使用 Win32 API 时,没有理由不将整数类型声明为 Long,即 32 位有符号整数,最大值为 2^31-1,即超过 20 亿。声明i As Long,问题解决!
  • 我将对此进行测试,谢谢 Mathieu。我会问另一个关于搜索大量收件箱的问题。
猜你喜欢
  • 2022-07-06
  • 1970-01-01
  • 2016-06-24
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 2017-04-08
  • 1970-01-01
相关资源
最近更新 更多