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:
Any idea? Every thread I have found all say to use CreateObject("Outlook.Application") but that has not worked either.
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.