none
.sendusingaccount Not Working RRS feed

  • Question

  • I am attempting to send e-mails in Access from other than my default account. Regardless of what I try, the default e-mail address is always used. I can use the other address by changing the send from manually. Here is an example of some code I have tried. Any idea of how to make this work.

    Sub doEmailOutlook()
    On Error Resume Next
    Dim oLook As Object
    Dim oMail As Object
    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.CreateItem(0)
    Set olaccount = oLook.account
    Set olAccounts = oLook.application.Session.accounts
    Set olAccountTemp = oLook.account
    Dim strEmail As String
    Dim strMsg As String
    Dim foundAccount As Boolean
    Dim strFrom As String
    foundAccount = False
    strFrom = "ar@spectroline.com"
    Set olAccounts = oLook.application.Session.accounts
         For Each olAccountTemp In olAccounts
            If (olAccountTemp.smtpAddress = strFrom) Then
                Set olaccount = olAccountTemp
                foundAccount = True
                Debug.Print olAccountTemp.smtpAddress
                Exit For
            End If
        Next
    'Test Values
    sTo = "<some e-mail address>"
    sSubject = "Test Message"
    sBody = "Test Message body"
    
    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.CreateItem(0)
    'Set olAccount = oLook.account
    With oMail
        .SendUsingAccount = "ar@spectroline.com"
        .ReplyRecipients.Add "accounts.receivable@spectroline.com"
        .To = sTo
        .HTMLBody = sBody
        .Subject = sSubject
        .SendUsingAccount = olaccount
        .From = olaccount
        .display
    End With
    

    Thursday, May 17, 2018 12:05 PM

All replies

  • Hi Roger,

    Thanks for visiting our forum. Then here we mainly focus on general issues about Outlook client. Since your query is more related to developing issues involving Outlook, I'll move your thread to the dedicated MSDN forum for Outlook for better response:

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=outlookdev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Yuki Sun


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, May 18, 2018 6:23 AM
  • Why are you setting SendUsingAccount to a string (which would produce an error)?

      .SendUsingAccount = "ar@spectroline.com"


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, May 18, 2018 6:30 AM
  • Hello Roger Cooper,

    I could reproduce your issue. It seems that setting SendUsingAccount property for a late binding mail item does not work. You could go to File->Feedback to submit a feedback for the issue.

    As as workaround, you need add reference to Outlook Interop and declare the oMail using early binding.

    Dim oMail As Outlook.MailItem

    By the way, as Dmitry said, you could not use a string to set SendUsingAccout. Please use the olaccount object you found to set the property.

    Best Regards,

    Terry


    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.

    Friday, May 18, 2018 9:13 AM
  • I meant to comment out <code>.SendUsingAccount ="ar@spectroline.com"</code> . I have tried this every way with, a search for the object, a string and a number with no effect. There is never an error message, it just uses the default e-mail.
    Friday, May 18, 2018 12:32 PM
  • As as workaround, you need add reference to Outlook Interop and declare the oMail using early binding.

    Dim oMail As Outlook.MailItem
    If I put that in, I get the error "user-defined type not defined"
    Friday, May 18, 2018 12:36 PM
  • Hello Roger Cooper,

    As I mentioned, it seems that we have to early binding the mail object. So we need add reference to Outlook Interop library first. Go to  Tools->Reference and then select Microsoft Outlook XX Object library, then you should be able to dim the Outlook.MailItem object.

    Best Regards,

    Terry


    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.

    Monday, May 21, 2018 2:57 AM
  • That helped. I am now running into a different issue.  I am receiving the error "object doesn't support this property or method" on "Set olAccount = oLook.account". What am I doing wrong?

    <code>

    Sub doEmailOutlook()
    Dim oLook As Object
    Dim oMail As Outlook.MailItem
    Dim olAccount As Variant
    Dim olAccounts As Variant
    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.CreateItem(0)
    Set olAccount = oLook.account
    Set olAccounts = oLook.application.Session.accounts
    Set olAccountTemp = oLook.account
    Dim strEmail As String
    Dim strMsg As String
    Dim foundAccount As Boolean
    Dim strFrom As String
    foundAccount = False
    strFrom = "<valid e-mail address>"
    Set olAccounts = oLook.application.Session.accounts
         For Each olAccountTemp In olAccounts
            If (olAccountTemp.smtpAddress = strFrom) Then
                Set olAccount = olAccountTemp
                foundAccount = True
                Debug.Print olAccountTemp.smtpAddress
                Exit For
            End If
        Next </code>

    Tuesday, May 22, 2018 6:02 PM
  • Of course. Outlook.Application object does not expose the Account property. Comment out that line.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, May 22, 2018 6:56 PM
  • What value should I use for .sendusingaccount? Or should I download your Outlook Redemption library and stop trying to do this with the outlook.application object?

    Tuesday, May 22, 2018 7:50 PM
  • Your code above looks fine. Just get rid of the problematic line and actually set the MailItem.SendUsingAccount property.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, May 22, 2018 7:59 PM
  • Hello Roger Cooper,

    See the code modified based on your code.

    Sub doEmailOutlook()
    Dim oLook As Object
    Dim oMail As Outlook.MailItem
    Dim olAccount As Variant
    Dim olAccounts As Variant
    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.CreateItem(0)
    Set olAccounts = oLook.Application.Session.accounts
    Dim strEmail As String
    Dim strMsg As String
    Dim foundAccount As Boolean
    Dim strFrom As String
    foundAccount = False
    strFrom = "<email address>"
    Set olAccounts = oLook.Application.Session.accounts
         For Each olAccountTemp In olAccounts
            If (olAccountTemp.smtpAddress = strFrom) Then
                Set olAccount = olAccountTemp
                foundAccount = True
                Debug.Print olAccountTemp.smtpAddress
                Exit For
            End If
        Next olAccountTemp
        
        'Test Values
    sTo = "<some e-mail address>"
    sSubject = "Test Message"
    sBody = "Test Message body"
    
    
    With oMail
        .ReplyRecipients.Add "accounts.receivable@spectroline.com"
        .To = sTo
        .HTMLBody = sBody
        .Subject = sSubject
        .SendUsingAccount = olAccount
        .Display
    End With
    End Sub
    

    Best Regards,

    Terry


    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.

    Wednesday, May 23, 2018 8:58 AM
  • Hi Terry

    I am struggeling on this for quite a while. As soon as I use .send instead of .display the default account is used. Any suggestions?

    Thanks

    Willi

    Thursday, December 20, 2018 7:47 AM