Consulting

Results 1 to 13 of 13

Thread: Solved: Can't VBA send emails when Outlook is not opened

  1. #1
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location

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

    Hello,

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

    [VBA]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[/VBA]

    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.
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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...


    [vba]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[/vba]

    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

  3. #3
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    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.
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Actually I have no problems sending emails from Outlook 2010 via vba from Excel. What is the error you get?

  5. #5
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    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.
    Attached Images Attached Images
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

  6. #6
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    I think I figured out what is happening.
    As Ken Slovak stated in http://help.lockergnome.com/office/a...ct1004220.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.
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You are correct that it does need to be open, but your code should be doing that anyway, with this...
    [vba] 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[/vba]
    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?

  8. #8
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    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?
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    It does, but I login usually when sending from my own email...

    [vba]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[/vba]

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

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Using your original code, adding .display makes your code work for me (2010). I had the same issue as you without it.

    [VBA]With OutMail
    .To = "name@domain.com"
    .Subject = "This is the Subject line"
    .Body = "Hi there"
    .display
    .Send
    End With[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Yeah, failed for me unless I logged on.

  12. #12
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    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.
    Last edited by Benzadeus; 02-22-2011 at 05:43 PM.
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

  13. #13
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Err... how to mark as solved? Couldn't find here.
    ---
    Felipe Costa Gualberto
    Microsoft Excel MVP
    http://www.ambienteoffice.com.br

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •