Discussion:
Outlook Fails to send when not visible
(too old to reply)
McKilty
2009-12-11 16:21:51 UTC
Permalink
I use this routine in several apps. If Outlook is not open on the
machine, it opens it behind the scenes and sends an e-mail, but the e-
mail stays in the drafts folder until I open Outlook manually.

I've tried DoEvents and a Sleep fucntion between the Send and the
Outlook Close, but it doesn't help. How can I alter this code that
that the e-mail ise sent.

********************************************************************************************
Public Sub Send_Auto_Email(Optional SendTo As String, Optional SendCc
As String, Optional SendBcc As String, Optional mySubject As String,
Optional myBody As String, Optional SendTime As Date, Optional
NextSendTime As Date, Optional SendOnBehalfOf As String, Optional AID
As Long, Optional EMPID As String)

Dim oItem, SafeItem, myNameSpace
Dim myOutlook As Outlook.Application
Dim blnWeCreated As Boolean

On Error Resume Next
blnWeCreated = False
Set myOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If myOutlook Is Nothing Then
blnWeCreated = True
Set myOutlook = New Outlook.Application
End If

Set myNameSpace = myOutlook.GetNamespace("MAPI")
myNameSpace.Logon

Set SafeItem = CreateObject("Redemption.SafeMailItem")
Set oItem = myOutlook.CreateItem(0)

SafeItem.Item = oItem 'set Item property
SafeItem.Subject = mySubject

SafeItem.To = SendTo
SafeItem.CC = SendCc
SafeItem.BCC = SendBcc

SafeItem.Recipients.ResolveAll

SaveSetting App.Title, "Log\" & EMPID & "\" & AID, "D) Subject",
mySubject
SaveSetting App.Title, "Log\" & EMPID & "\" & AID, "E) To", SendTo
SaveSetting App.Title, "Log\" & EMPID & "\" & AID, "F) CC", SendCc
SaveSetting App.Title, "Log\" & EMPID & "\" & AID, "G) BCC", SendBcc

If Len(SendOnBehalfOf) > 0 Then
SafeItem.SentOnBehalfOfName = SendOnBehalfOf
End If

SafeItem.Body = myBody & EmailFooter(SendTime, NextSendTime)
SafeItem.Send

If blnWeCreated = True Then
myOutlook.Quit
End If

Set oItem = Nothing
Set SafeItem = Nothing
Set myOutlook = Nothing

Exit Sub

ErrorHandler:
SaveSetting App.Title, "Log\" & EMPID & "\" & AID & "\Error",
"Number", Err.Number
SaveSetting App.Title, "Log\" & EMPID & "\" & AID & "\Error",
"Description", Err.Description

If Err.Description = "You do not have the permission to send the
message on behalf of the specified user." Then
SafeItem.SentOnBehalfOfName = ""
SafeItem.Body = myBody
SafeItem.Body = SafeItem.Body & vbCrLf & vbCrLf & vbCrLf _
&
"*************************************************************************"
& vbCrLf _
& "THIS E-MAIL WAS SUPPOSED TO BE SENT ON BEHALF OF ' " & UCase
(SendOnBehalfOf) & " ' , BUT THE SQL ADMIN HAS NOT BEEN GIVEN THOSE
RIGHTS."
SafeItem.Body = SafeItem.Body & EmailFooter(SendTime, NextSendTime)
SafeItem.BCC = SendBcc & "; " & SendOnBehalfOf
Resume
Else
MsgBox Err.Number & " " & Err.Description
Exit Sub

End If

End Sub
********************************************************************************************
McKilty
2009-12-11 16:24:20 UTC
Permalink
I should have mentioned that this is a VB6 application, not VBA.
Ken Slovak - [MVP - Outlook]
2009-12-11 22:12:01 UTC
Permalink
What version of Outlook? Use SyncObjects.Item(1).Start if it's in the object
model for your version. If it's Outlook 2000 use
Redemption.MAPIUtils.DeliverNow(). See if that helps. You might have to show
an Explorer though.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by McKilty
I should have mentioned that this is a VB6 application, not VBA.
McKilty
2009-12-17 04:32:59 UTC
Permalink
It's Outlook 2003.

Thanks, I will try that.

I may need help showing an explorer. I thought there'd be a .Display
or .Visible, but I think I checked for those and didn't find
anything. It's been a while since I touched it now and I'm at home
away from the code.

On Dec 11, 5:12 pm, "Ken Slovak - [MVP - Outlook]"
Post by Ken Slovak - [MVP - Outlook]
What version of Outlook? Use SyncObjects.Item(1).Start if it's in the object
model for your version. If it's Outlook 2000 use
Redemption.MAPIUtils.DeliverNow(). See if that helps. You might have to show
an Explorer though.
--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.http://www.slovaktech.com/products.htm
Post by McKilty
I should have mentioned that this is a VB6 application, not VBA.
Ken Slovak - [MVP - Outlook]
2009-12-17 14:31:14 UTC
Permalink
To get an Explorer object you can use MAPIFolder.GetExplorer(). To show the
Explorer you use the Explorer.Display() method.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"McKilty" <***@gmail.com> wrote in message news:ddec82ba-5d3d-4c2f-92c6-***@3g2000vbp.googlegroups.com...
It's Outlook 2003.

Thanks, I will try that.

I may need help showing an explorer. I thought there'd be a .Display
or .Visible, but I think I checked for those and didn't find
anything. It's been a while since I touched it now and I'm at home
away from the code.
Loading...