Check if Outlook is open, if not then open

tmyers

Well-known member
Local time
Yesterday, 23:16
Joined
Sep 8, 2020
Messages
1,091
I have a module that sends emails when a task is complete and it works fine as long as Outlook is already open and running. However when it is not, I get run time error 429. To handle this, I have tried starting Outlook but it never seems to actually start:
Code:
Public Sub SendAcknowledgement()

Dim Started         As Boolean
Dim OutLookApp      As Outlook.Application
Dim oItem           As Outlook.MailItem
Dim sEmail          As String
Dim sSender         As String

On Error GoTo Handler

Set OutLookApp = GetObject(, "Outlook.Application")


sSender = Nz(DLookup("WrittenBy", "tblTicket", "TicketID=" & Forms!frmWireRoom.TicketID), "0")
sEmail = Nz(DLookup("Email", "tblUsers", "Username ='" & sSender & "'"), "Error")

If sSender = 0 Then
    Exit Sub
End If

If sEmail = Error Then
    MsgBox "No destination email in system. Operation has been cancelled and no email will be sent.", vbOKOnly
    Exit Sub
End If


'Open new message
Set oItem = OutLookApp.CreateItem(olMailItem)
With oItem
    .To = sEmail
    .Subject = "Ticket # " & Forms.frmWireRoom.TicketNum & " for " & [Reports]![rptWireRoom]![CustomerID]
    .Body = "The cuts for ticket # " & Forms.frmWireRoom.TicketNum & " are complete. This is an automated message sent via ReelTime."
    .Send
End With

'clean up
Set oItem = Nothing
Set OutLookApp = Nothing

Handler:
Select Case Err.Number

    Case 429
        If OutLookApp Is Nothing Then
            Set OutLookApp = New Outlook.Application
        End If
        Resume
End Select

End Sub

Any idea? Every thread I have found all say to use CreateObject("Outlook.Application") but that has not worked either.
 
Reading now. Will post wat comes of it. Thank you!
 
No problem. Everything I've tried of Daniel's has worked well.
 
In brief, test the object you got from the GetObject. If object IS NOTHING (which involves a keyword phrase) is TRUE then you need to CREATEOBJECT.
 
I just used
Set objOutlook = CreateObject("Outlook.Application")
If already open it puts it in the Outbox, if not open it still puts it in the Outbox ?
 
For what it is worth, I had a lot of trouble with this too and finally found this that works for me. It is a module that tests for Outlook being open. If not then Outlook is opened.
Code:
Sub TestOutlookIsOpen()
Dim A, B, C, D As String
A = "Outlook Is Not Open, Outlook Will Now Open."
B = "Please Wait Until Outlook Is Fully Open."
C = "This May Take A Minute or More."
D = A & Chr(10) & B & Chr(10) & C

Dim oOutlook As Object
    On Error Resume Next
        Set oOutlook = GetObject(, "Outlook.Application")
            On Error GoTo 0
    If oOutlook Is Nothing Then
        MsgBox D
  Shell "Outlook.exe"
    End If
End Sub
 
You are not dimming your variables correctly. :(
Only D is a string type variable.
 
Sorry for not checking back, was on vacation all last week :giggle:.

Doing what Doc suggested and checking if the object is nothing did the trick in getting the code to work correctly.
 

Users who are viewing this thread

Back
Top Bottom