none
Automating Outlook, MailItem not sending unless Outlook UI is open RRS feed

  • Question

  • Hello, 

    I am working with Office 2010 and am trying to automate Outlook using Access VBA.  My goal is to open a study participant record in our Access app in a form and then create a personalized email for that participant from an Outlook message template by clicking a button on that form.  The user can then review the email for any potential errors prior to sending.  I have code on my Access form which creates an Outlook MailItem from my template, replaces some parameterized text (eg participant last name, some dates, etc) and displays the resulting message, all of which works well.  My problem is that the message does not get sent when the user clicks on the message's Send button unless the full Outlook UI is currently open.  My stripped down code which illustrates the problem is below.  BTW I put the MsgBox in there thinking that the problem was that parent objects to the MailItem were getting destroyed before the message was sent but this does not appear to have any effect.

    Sub NoSendy()

        Const SEND_AS As String = "StudyGroup@uams.edu"
        Const SUBJECT_LINE As String = "Study Questionnaire"
        Const olMailItem As Long = 0

        Dim objOutlook As Object
        Dim objMsg As Object
        Dim objRecipient As Object

        Dim strLocalPath As String
        
        strLocalPath = Path_get(CurrentDb.Name)

        Set objOutlook = CreateObject("Outlook.Application")
        Set objMsg = objOutlook.CreateItemFromTemplate(strLocalPath & "ocq_intro.oft")
        
        objMsg.Subject = SUBJECT_LINE
        objMsg.SentOnBehalfOfName = SEND_AS

        Set objRecipient = objMsg.Recipients.Add("test_recipient@emailaddress.com")

        For Each objRecipient In objMsg.Recipients
            objRecipient.Resolve
        Next

        objMsg.Display
        MsgBox "Click Ok to continue."

    CleanUp:

        Set objOutlook = Nothing
        Set objMsg = Nothing
        Set objRecipient = Nothing

    End Sub

    Thursday, January 12, 2017 4:09 PM

Answers

  • Hi Bruce,

    I made a test with your code under Outlook 2013, and I could reproduce your issue. Based on our test, it seems we need an Explorer to send the email.

    For a workaround, I suggest you try something like below which adds a new invisible Explorer while sending an email.

    Sub NoSendy()
         Const SEND_AS As String = "xx@microsoft.com"
         Const SUBJECT_LINE As String = "Study Questionnaire1"
         Const olMailItem As Long = 0
         Dim objOutlook As Outlook.Application
         Dim objMsg As Outlook.MailItem
         Dim objRecipient As Object
         Dim strLocalPath As String
         'strLocalPath = Path_get(CurrentDb.Name)
         Set objOutlook = CreateObject("Outlook.Application")
         Set myExplorers = objOutlook.Explorers
         Set myFolder = objOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
         Set myOlExpl = myExplorers.Add(myFolder, olFolderDisplayNoNavigation)
         'Set objMsg = objOutlook.CreateItemFromTemplate(strLocalPath & "ocq_intro.oft")
         Set objMsg = objOutlook.CreateItem(olMailItem)
         objMsg.Subject = SUBJECT_LINE
         objMsg.SentOnBehalfOfName = SEND_AS
         Set objRecipient = objMsg.Recipients.Add("xx@microsoft.com")
         For Each objRecipient In objMsg.Recipients
             objRecipient.Resolve
         Next
         objMsg.Display
         MsgBox "Click Ok to continue."
    CleanUp:
         Set objOutlook = Nothing
         Set objMsg = Nothing
         Set objRecipient = Nothing
         Set myExplorers = Nothing
         Set myFolder = Nothing
         Set myOlExpl = Nothing
     End Sub

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Bruce Hulsey Wednesday, January 18, 2017 4:55 PM
    Monday, January 16, 2017 8:29 AM

All replies

  • Hello Bruce,

    First of all, try to declare the Outlook Application instance at the global scope (not method).

    Did you try to use the Send method instead of displaying the mail item to a user?

    In that case most probably the message is not sent because Outlook triggers a security prompt. Read more about that in the Outlook "Object Model Guard" Security Issues for Developers article.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, January 12, 2017 4:24 PM
  • Hi Eugene,

    I tried declaring objOutlook at the global level which made no difference.

    I did not try using Send method because I need the user to be able to review and decide whether to send.

    There is no Outlook security prompt triggered because the Send method is not invoked.

    Also, I am not using SMTP because there is a lot of formatting in the message and it was easier to set that up with an Outlook message template.

    Thanks!

    -Bruce

    Thursday, January 12, 2017 5:32 PM
  • Hi Bruce,

    I made a test with your code under Outlook 2013, and I could reproduce your issue. Based on our test, it seems we need an Explorer to send the email.

    For a workaround, I suggest you try something like below which adds a new invisible Explorer while sending an email.

    Sub NoSendy()
         Const SEND_AS As String = "xx@microsoft.com"
         Const SUBJECT_LINE As String = "Study Questionnaire1"
         Const olMailItem As Long = 0
         Dim objOutlook As Outlook.Application
         Dim objMsg As Outlook.MailItem
         Dim objRecipient As Object
         Dim strLocalPath As String
         'strLocalPath = Path_get(CurrentDb.Name)
         Set objOutlook = CreateObject("Outlook.Application")
         Set myExplorers = objOutlook.Explorers
         Set myFolder = objOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
         Set myOlExpl = myExplorers.Add(myFolder, olFolderDisplayNoNavigation)
         'Set objMsg = objOutlook.CreateItemFromTemplate(strLocalPath & "ocq_intro.oft")
         Set objMsg = objOutlook.CreateItem(olMailItem)
         objMsg.Subject = SUBJECT_LINE
         objMsg.SentOnBehalfOfName = SEND_AS
         Set objRecipient = objMsg.Recipients.Add("xx@microsoft.com")
         For Each objRecipient In objMsg.Recipients
             objRecipient.Resolve
         Next
         objMsg.Display
         MsgBox "Click Ok to continue."
    CleanUp:
         Set objOutlook = Nothing
         Set objMsg = Nothing
         Set objRecipient = Nothing
         Set myExplorers = Nothing
         Set myFolder = Nothing
         Set myOlExpl = Nothing
     End Sub

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Bruce Hulsey Wednesday, January 18, 2017 4:55 PM
    Monday, January 16, 2017 8:29 AM
  • Hi Edward, 

    That seems to work properly.  Thanks!

    -Bruce

    Wednesday, January 18, 2017 4:55 PM