PDA

View Full Version : Solved: Can't VBA send emails when Outlook is not opened



Benzadeus
02-18-2011, 10:57 AM
Hello,

I've used the simple code below a lot of times succesfully:

Sub SendVBAMail()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0

Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
.To = "name@domain.com"
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send
End With

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

When Outlook is opened, the code runs fine.
The fact is that when Outlook is closed, a new instance of it will be created and I get an error at .Send method. Any ideas? In Office 2003 this code worked fine.

*I tried executing it in Windows 7, Office 2010/32 and 64 bits (different computers). I tried late binding it too. I think it is a security issue from Outlook 2010.

Zack Barresse
02-20-2011, 10:25 AM
Yes, you can do that. Since you're opening Outlook, and you're opening it if it's not already open, you should clean up behind yourself. Also, you're using the Send method. This will generally prompt in a security warning, and it doesn't look like you're using Outlook Redemption or ClickYes. Anyway, your base code would look like this...


Sub SendVBAMail()

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim blnOLOpen As Boolean

On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
blnOLOpen = True
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
blnOLOpen = False
End If
On Error GoTo 0

Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
.To = "name@domain.com"
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send
End With

If blnOLOpen = False Then OutApp.Quit

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

I would recommend ClickYes, it's very easy to install, free, and easy to code. Here is a good example of how it's used...

http://www.excelguru.ca/node/44

HTH

Benzadeus
02-20-2011, 10:48 AM
This will generally prompt in a security warning
Yes, on earlier versions of Outlook I got a security warning, but it is easy to bypass it.
The problem is that with Outlook 2010 I get an error on runtime at the .Send method.

In fact, I simply can't send an email from any Office Application when Outlook 2010 is closed.

Zack Barresse
02-20-2011, 10:51 AM
Actually I have no problems sending emails from Outlook 2010 via vba from Excel. What is the error you get?

Benzadeus
02-21-2011, 08:42 AM
I'm getting the following error on a Win7-Office/2010 32 bits and other 64 bits, on the Send method, as showed in attached image.

My Office is Portuguese-Brazil. Translating to english, the description of the error would be Application-defined or object-defined error

Note that when Outlook Application Object is created dynamically, a small gear appears in the tray icon.

Benzadeus
02-21-2011, 09:19 AM
I think I figured out what is happening.
As Ken Slovak stated in http://help.lockergnome.com/office/automatically-emails-MS-Access-2007--ftopict1004220.html, "You can safely assume that if Outlook is not running you can't use it for anything.".

It was not very clear to me if this is a 2007-2010 security issue, but the way I'll fix that is testing if Outlook is opened. If not, a message to the user will appear asking him to do it.

Thanks for your time.

Zack Barresse
02-21-2011, 03:15 PM
You are correct that it does need to be open, but your code should be doing that anyway, with this...
On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
End If
On Error Goto 0
I changed the code slightly to add a boolean variable in the case that if Outlook wasn't opened before the code ran, it would likewise be closed after the code ran. You shouldn't be getting an application error though, that is just strange. When you step through your code (with F8) does an Outlook object not get created?

Benzadeus
02-22-2011, 04:21 AM
I changed the code slightly to add a boolean variable in the case that if Outlook wasn't opened before the code ran
Yes, I usually do that on my code, as it is a programming good practice.


You shouldn't be getting an application error though, that is just strange
On earlier versions (2003) of Outlook, I didn't get Application Error. And the code just worked fine.


When you step through your code (with F8) does an Outlook object not get created?
It gets created, but the icon of the new Outlook instance looks like the one I attached on post #5.

Question: If you execute my code with Outlook closed, can you send e-mails or not? What's your Outlook version?

Zack Barresse
02-22-2011, 12:08 PM
It does, but I login usually when sending from my own email...

Sub SendVBAMail()

Dim OutApp As Outlook.Application
Dim OutNS As Outlook.Namespace
Dim OutMail As Outlook.MailItem
Dim blnOLOpen As Boolean

On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
blnOLOpen = True
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
blnOLOpen = False
End If
On Error GoTo 0
Set OutNS = OutApp.GetNamespace("MAPI")
OutNS.Logon Profile:="PROFILE NAME, USUALLY 'Outlook'", Password:="password", ShowDialog:=False, NewSession:=True

Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
.To = "email@domain.com"
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send
End With
OutNS.Logoff

If blnOLOpen = False Then OutApp.Quit

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

Change the information above to match your information. Works for me. And I'm using 2010 btw.

mdmackillop
02-22-2011, 03:35 PM
Using your original code, adding .display makes your code work for me (2010). I had the same issue as you without it.

With OutMail
.To = "name@domain.com"
.Subject = "This is the Subject line"
.Body = "Hi there"
.display
.Send
End With

Zack Barresse
02-22-2011, 03:36 PM
Yeah, failed for me unless I logged on.

Benzadeus
02-22-2011, 05:28 PM
Good workaround, but now I got another error from Zack's and Malcolm's code (same error from both codes).

It's not a VBA runtime error, but a popup warning from Outlook stating that "The Sent Items Folder is not avaible" and the Application doesn't send the e-mail.

Any ideas?


***EDIT***
I got this thred solved by both your code. I was getting that popup because of my permissions.

Thank you all.

Benzadeus
02-22-2011, 05:45 PM
Err... how to mark as solved? Couldn't find here.