Check if Outlook is open, if not then open (1 Viewer)

tmyers

Well-known member
Local time
Today, 01:57
Joined
Sep 8, 2020
Messages
1,090
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.
 

tmyers

Well-known member
Local time
Today, 01:57
Joined
Sep 8, 2020
Messages
1,090
Reading now. Will post wat comes of it. Thank you!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:57
Joined
Aug 30, 2003
Messages
36,125
No problem. Everything I've tried of Daniel's has worked well.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:57
Joined
Feb 28, 2001
Messages
27,186
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:57
Joined
Sep 21, 2011
Messages
14,299
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 ?
 

Eljefegeneo

Still trying to learn
Local time
Yesterday, 22:57
Joined
Jan 10, 2011
Messages
904
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:57
Joined
Sep 21, 2011
Messages
14,299
You are not dimming your variables correctly. :(
Only D is a string type variable.
 

tmyers

Well-known member
Local time
Today, 01:57
Joined
Sep 8, 2020
Messages
1,090
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

Top Bottom