A tricky one... Bizarre!!!

A

Albert

Hello!
I have made a procedure that sends Emails from Excel (2003) through the
e-mail account I have set up in Outlook (2007). The procedure sends the whole
workbook as an attachment. I have added the procedure, in an independent
module, to two different workbooks, and its working fine.

Here's the catch:

In one of the workbooks, when I run the procedure it sends the e-mail
immediately.
In the other workbook, however, it does not send the e-mail UNTIL I open
Outlook!
The e-mail sending module is identical in both workbooks.
Does anyone know why this happens?
Is there any way to force the procedure to send the e-mail immediately
without opening Outlook?
I thank you in advance,
Lazy Albert

Check out the code:
I have made a Reference to Outlook 12 in both workbooks

Sub SendEmail(DireccionEmail, CuerpoEmail, SubjectEmail)

Dim bOutlookStarted As Boolean
Dim bTerminateMerge As Boolean
Dim intSourceRecord As Integer
Dim objMailItem As Outlook.MailItem
Dim objOutlook As Outlook.Application
Dim strMailSubject As String
Dim strMailTo As String
Dim strMailBody As String
Dim strOutputDocumentName As String

bOutlookStarted = False

Set objMerge = ActiveWorkbook

' Start Outlook as necessary
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set objOutlook = CreateObject("Outlook.Application")
bOutlookStarted = True
End If

With objMerge
strMailSubject = SubjectEmail
strMailBody = CuerpoEmail
strOutputDocumentName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
strMailTo = DireccionEmail
Set objMailItem = objOutlook.CreateItem(olMailItem)
With objMailItem
.Subject = strMailSubject
.Body = strMailBody
.To = strMailTo
.CC = "(e-mail address removed)" 'Me mando una copia
.Attachments.Add strOutputDocumentName, olByValue, 1
.Send
End With
Set objMailItem = Nothing
End With

MsgBox "E-mail enviado con éxito."

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top