【问题标题】:Handle error when Outlook is not availableOutlook 不可用时处理错误
【发布时间】:2020-03-18 10:21:00
【问题描述】:

我有一个可以在办公室工作的宏。

从远程系统工作时,我们没有 Outlook,它会生成一个错误,即无法创建 Outlook 邮件。

我需要一个 MsgBox,在 Remote 上显示没有 Outlook,然后退出 sub。

Sub Mail_workbook_Outlook_1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim bodystr As String
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    bodystr = "Test"
    
    ActiveWorkbook.Save

    On Error Resume Next
    
    With OutMail
        .To = Worksheets("Test").Range("D25")
        .CC = Worksheets("Test").Range("D26")
        .BCC = ""
        .Subject = Worksheets("Test").Range("D10")
        .HTMLbody = bodystr
        .Attachments.Add ActiveWorkbook.FullName
        .Send  
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

我试过了:

    Set OutApp = CreateObject("Outlook.Application")
    If OutApp Is Nothing Then
        MsgBox "Outlook is not open, Open Outlook and try again!"
        Exit Sub
    Else
        Set OutMail = OutApp.CreateItem(0)
    End If
    
    bodystr = "Test"
        
    ActiveWorkbook.Save
    
    On Error Resume Next
        
    With OutMail
        .To = Worksheets("Test").Range("D25")
        .CC = Worksheets("Test").Range("D26")
        .BCC = ""
        .Subject = Worksheets("Test").Range("D10")
        .HTMLbody = bodystr
        .Attachments.Add ActiveWorkbook.FullName
        .Send  
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    请试试这个代码:

       On Error Resume Next
         Set OutApp = CreateObject("Outlook.Application")
         Set Outmail = Outapp.Createitem(0)
        If Err <> 0 Then
            Err.Clear: On Error GoTo 0
            MsgBox "No  Outlook Application installed, or not configured": Exit Sub
        End If
        On Error GoTo 0
    

    【讨论】:

    • 我添加了这个,但随后宏在 Set Outmail = Outapp.Createitem(0) 行再次失败。
    • 这意味着 Outlook 已安装!它没有配置...我会改变它来解决这个问题。
    • 如果安装了应用程序,是否也可以先查看,如果没有,然后获取 msgbox?我们正在使用 2 个选项,一台普通电脑,然后是 Outlook 和 Remote,然后没有 Outlook 应用程序。
    • @Karel Jansen:是的,我们必须检查这两行。我会修改代码...
    • @fanuDuru,你能帮我解决这个问题吗,我已经花了好几个小时了,但我想不通哈哈!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2020-04-04
    相关资源
    最近更新 更多