【问题标题】:Outlook mail intermittently not sent using Excel VBAOutlook 邮件间歇性未使用 Excel VBA 发送
【发布时间】:2020-09-22 15:13:06
【问题描述】:

通过 Outlook 发送时遇到间歇性 Excel VBA 问题。

我的客户每天发送 20 或 30 次电子邮件文件。大约每周一次,电子邮件不发送。它不在发件箱或已发送。我们只有在收到没有相应电子邮件的实物样品时才会知道,通常是第二天。

相关的Excel VBA代码:

'[bunch of code that sets everything up in order to send]    

Err.Clear
On Error Resume Next
Set OutMail = oOutlook.CreateItem(0) ' [oOutlook is a previously created object which is an instance of Outlook]
With OutMail
    .To = Address ' [Address=email I have pre-filled]
    .Subject = "Swabs File"
    .body = vbAns ' [vbAns = variant I have pre-filled]
    .Attachments.Add (Dir) ' [Dir = string which is the name of the file]
    .send
End With

'Error trap if there is any problem at all
If Err Then
    MsgBox ("Problem with Outlook - Failed to Send. Please try again")
    Exit Sub
else
    MsgBox ("Your file has been sent")
End If

当问题发生时,错误没有被捕获,客户端认为他们已经成功发送了文件。

如何捕获错误?

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:
    Option Explicit
    
    Private Sub test()
    
        Dim oOutlook As Object
        Dim OutMail As Object
        
        ' *** with Outlook ***
        ' As many times as you want there will only be one
        'Set oOutlook = New outlook.Application
        
        'On Error Resume Next   ' generate mysterious failure,
        '                          hide error so developer cannot debug
        
        On Error GoTo trap      ' Error trap if there is any problem at all
        
        If Not oOutlook Is Nothing Then ' confirm Outlook is available
        
            Set OutMail = oOutlook.CreateItem(0)
            
            With OutMail
            
                '.To = Address ' [Address=email I have pre-filled]
                .Subject = "Swabs File"
                '.body = vbAns ' [vbAns = variant I have pre-filled]
                
                Error 5   ' for demonstration
                .Attachments.Add (dir) ' [Dir = string which is the name of the file]
                
                .Display    '.Send
                
            End With
            
            MsgBox "Your file has been sent"
            
        Else
        
            MsgBox "oOutlook object does not exist - Failed to Send."
            
        End If
        
        Exit Sub    ' <---  bypass the trap if no error
        
    
    trap:   'Error trap if there is any problem at all
        
        MsgBox "Problem with Outlook - Failed to Send. Please try again"
    
    End Sub
    

    仅在“You know why the error occurs.”时使用On Error Resume Next
    您可以采取措施解决错误,也可以故意忽略它。

    There is a general method of usingOn Error Resume Next.

    【讨论】:

    • 感谢您的帮助 niton - 我最终完全删除了所有错误检测,以便在出现问题时代码会崩溃 - 它没有。电子邮件发送了无数次,然后失败了一次,但没有错误消息。我想我要做的就是进入 Outlook 上的“已发送”框,看看我的电子邮件是否在那里 - 如果它很好,请再试一次。展望未来,我会将您的错误捕获方法用于其他代码。再次感谢。
    猜你喜欢
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    相关资源
    最近更新 更多