SendUsingAccount (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 17:35
Joined
Sep 21, 2011
Messages
14,270
Hi all,

I now have the need to select a different email address when sending out emails.
I have rewritten my code to cater for this and failing at the SendUsingAccount.

In the debug window I have the correctly named account, however when I send the email it goes under my default email address, not the one selected for SendUsingAccount.?

I followed the logic from https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/mailitem-sendusingaccount-property-outlook

My code is below
Code:
    Dim OutApp As Object, OutMail As Object, OutAccount As Object
    Dim strSignature As String, strBody As String, strDiv As String
    Dim strGreeting As String, strTimeLimit As String
    Dim iTime As Integer
    
    strTimeLimit = "Please note, invoices should be submitted by 10 AM the following Friday to ensure prompt payment."
    strTimeLimit = strTimeLimit & " Invoices received after this time will be paid the following week."
    
    strGreeting = "Good "
    iTime = Val(Format(Now(), "hh"))
  
    Select Case iTime
    Case Is < 12
        strGreeting = strGreeting & "morning,"
    Case Is < 17
        strGreeting = strGreeting & "afternoon,"
    Case Else
        strGreeting = strGreeting & "evening,"
    End Select

    strDiv = "<div class=WordSection1>"
    strBody = "<p>" & strGreeting & "</p>" & pstrMessage
    ' Now comes from email table
    'strBody = strBody & "<p>" & strTimeLimit & "</p><p>"
    strBody = strBody & "<p>Should there be any queries please let me know.</p>"

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    ' Need to get correct account to send on
    For Each OutAccount In OutApp.Session.Accounts
        If OutAccount.DisplayName = pstrCompany Then
            Exit For
        End If
    Next

    With OutMail
        .Display
        .To = pStrToNames
        .To = "aaabbb@ccc.co.cuk"
        .BCC = ""
        .Subject = pstrSubject
        .htmlbody = Replace(.htmlbody, strDiv, strDiv & strBody)
        .attachments.Add pstrFilename
        Debug.Print OutAccount.DisplayName
        .sendusingaccount = OutAccount
        .Send
    End With
and the correct account name is shown with the debug statement?, however I notice the Intellisense has not corrected .SendUsingAccount ?

Again, what am I missing please.?
 

isladogs

MVP / VIP
Local time
Today, 17:35
Joined
Jan 14, 2017
Messages
18,216
Its easy to specify a different send address when using CDO to send email as long as its valid
You can also send with one address but have a different address displayed to the recipient
As well as the link Minty provided (to which we both contributed) have a look at this utility: https://www.access-programmers.co.uk/forums/showthread.php?t=293368
 

June7

AWF VIP
Local time
Today, 08:35
Joined
Mar 9, 2014
Messages
5,470
OutAccount is declared as an object. Possibly need a property of the object, something like OutAccount.Address? Do a test with static string address.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 17:35
Joined
Sep 21, 2011
Messages
14,270
Uurgh,
I even have a Thanks in that thread.


Minty,
I have this and the other accounts in my Outlook.
I tried Roger's solution using the item number instead, but no difference.:banghead:
Code:
  ' Need to get correct account to send on
    For Each OutAccount In OutApp.Session.Accounts
        iAccount = iAccount + 1
        If OutAccount.DisplayName = pstrCompany Then
            Exit For
        End If
    Next

    On Error Resume Next
   ' Change the mail address and subject in the macro before you run it.
'    With OutMail
'        .Display
'    End With
    With OutMail
        .Display
        .To = pStrToNames
        .To = "aaaa@bbbccc.co.uk"
        .BCC = ""
        .Subject = pstrSubject
        .htmlbody = Replace(.htmlbody, strDiv, strDiv & strBody)
        '.attachments.Add pstrFilename & ".pdf
        .attachments.Add pstrFilename
        Debug.Print OutAccount.DisplayName
        '.sendusingaccount = OutAccount
        OutMail.sendusingaccount = OutApp.Session.Accounts.Item(iAccount)
        .Send
    End With


Why is Intellisense not correcting sendusingaccount for proper case?


Off home now, will have at it again tomorrow.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:35
Joined
Sep 21, 2011
Messages
14,270
I should perhaps add that we do not have an exchange server, merely external server accounts.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:35
Joined
Sep 21, 2011
Messages
14,270
OutAccount is declared as an object. Possibly need a property of the object, something like OutAccount.Address? Do a test with static string address.

Hi June7,

Will try tomorrow, however I did try to follow the MS code that I linked to in the first post.?
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:35
Joined
Sep 21, 2011
Messages
14,270
OK, I believe I now have it working.

To do so I had to change
Dim OutApp As Object, OutMail As Object, OutAccount As Object
to
Dim OutApp As Outlook.Application, OutMail As Outlook.Mailitem, OutAccount As Outlook.Account
and add the reference to the Outlook library


I then had to set the account BEFORE I displayed the email.
Code:
    For Each OutAccount In OutApp.Session.Accounts
        iAccount = iAccount + 1
        If OutAccount.DisplayName = pstrCompany Then
            Exit For
        End If
    Next

    On Error Resume Next
   ' Change the mail address and subject in the macro before you run it.
    With OutMail
        .SendUsingAccount = OutAccount
         'Debug.Print OutAccount.DisplayName & " & " & OutAccount.smtpAddress
         
        .Display
I am only using local Outlook accounts, no exchange server, so although I also tried SendOnBehalfOfName, it did not make any difference.
I do not know why I was using late binding for the email. No doubt I will find out sometime in the future when something goes wrong and then I will hopefully remember. :D


Thank you all that replied.
 

Minty

AWF VIP
Local time
Today, 17:35
Joined
Jul 26, 2013
Messages
10,371
The late binding is best if you distribute to users who might not be using the same office version as you.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:35
Joined
Sep 21, 2011
Messages
14,270
Hi Minty,
We all have the same old version of Office.
FWIW it is only really myself that runs this. If I am away, they can use my PC with my instructions, and besides I could not get it to work on late binding.?
I'd love to be proved wrong though, if only to learn something new, like I have just done in the last day or so with this SendUsingAccount.

I've seen posts on it previously and even applied a thanks to the post you supplied a link to, but implementing it correctly is another matter. :D
 

Minty

AWF VIP
Local time
Today, 17:35
Joined
Jul 26, 2013
Messages
10,371
If it's not distributed then early binding is simplest, as the intellisense kicks in.

Some people develop with early binding the switch to late for distribution versions.
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:35
Joined
Sep 21, 2011
Messages
14,270
I should have realised that, when the code was not being set to proper text and I cannot remember why I took that route.
I know I took that code from my Excel workbook when it was running using Excel, perhaps I just copied as is from the net?

Only time will tell.

Now I need to amend the instructions. ;)

If it's not distributed then early binding is simplest, as the intellisense kicks in.

Some people develop with early binding the switch to late for distribution versions.
 

Users who are viewing this thread

Top Bottom