Karel Jansen
Karel Jansen

Reputation: 63

Handle error when Outlook is not available

I have a macro that works when I am at the office.

When working from Remote system we don't have Outlook and it will generate a error that it isn't possible to create an Outlook mail.

I need a MsgBox that says on the Remote there is no Outlook and then exit 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

I tried:

    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

Upvotes: 0

Views: 322

Answers (1)

FaneDuru
FaneDuru

Reputation: 42236

Try this code, please:

   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

Upvotes: 1

Related Questions