I have the code below which works flawlessly when Outlook is open, but if Outlook is closed, it sends the message the next time I open Outlook, not immediately. By the way, I am using redemption. Any suggestions? Thanks
Public Sub MailIt(name As String)
Dim olItem As Outlook.MailItem
Dim olApp As Outlook.Application
Dim SafeMail, oItem
Dim isOpen As Boolean
Dim body As Variant
Dim olNamespace As Variant
On Error Resume Next
isOpen = True 'initialize boolean variable
'try to grab current instance of Outlook
Set olApp = GetObject(, "Outlook.Application"
'if Outlook isn't open, boolean is false, create instance of Outlook
If olApp Is Nothing Then
isOpen = False
Set olApp = CreateObject("Outlook.Application"
End If
On Error GoTo 0
'Create an instance of Redemption.SafeMailItem
Set SafeMail = CreateObject("Redemption.SafeMailItem"
Set olItem = olApp.CreateItem(olMailItem)
SafeMail.Item = olItem
'set variable "body" to text in textbox
body = Forms!frmAutoRun!txtResults.Value
With SafeMail
.Recipients.Add "test@email.com" 'Addresses separated by ;
.Subject = "Allmid EOM Archive Results" 'Subject
.body = body & vbCrLf & vbCrLf 'Body
.Attachments.Add name 'attachments
.Send 'send email
End With
Ex: On Error Resume Next
Set olItem = Nothing
Set SafeMail = Nothing
' if isOpen is False, then close Outlook
'Returns outlook to the same state as when
'this function started
If Not isOpen Then
olApp.Quit
Set olApp = Nothing
End If
Exit Sub
Er:
MsgBox "MailIt: " & Err & " " & Err.Description
GoTo Ex
End Sub
Public Sub MailIt(name As String)
Dim olItem As Outlook.MailItem
Dim olApp As Outlook.Application
Dim SafeMail, oItem
Dim isOpen As Boolean
Dim body As Variant
Dim olNamespace As Variant
On Error Resume Next
isOpen = True 'initialize boolean variable
'try to grab current instance of Outlook
Set olApp = GetObject(, "Outlook.Application"
'if Outlook isn't open, boolean is false, create instance of Outlook
If olApp Is Nothing Then
isOpen = False
Set olApp = CreateObject("Outlook.Application"
End If
On Error GoTo 0
'Create an instance of Redemption.SafeMailItem
Set SafeMail = CreateObject("Redemption.SafeMailItem"
Set olItem = olApp.CreateItem(olMailItem)
SafeMail.Item = olItem
'set variable "body" to text in textbox
body = Forms!frmAutoRun!txtResults.Value
With SafeMail
.Recipients.Add "test@email.com" 'Addresses separated by ;
.Subject = "Allmid EOM Archive Results" 'Subject
.body = body & vbCrLf & vbCrLf 'Body
.Attachments.Add name 'attachments
.Send 'send email
End With
Ex: On Error Resume Next
Set olItem = Nothing
Set SafeMail = Nothing
' if isOpen is False, then close Outlook
'Returns outlook to the same state as when
'this function started
If Not isOpen Then
olApp.Quit
Set olApp = Nothing
End If
Exit Sub
Er:
MsgBox "MailIt: " & Err & " " & Err.Description
GoTo Ex
End Sub