Setting the Email address to send an outlook email

john_willmott

New member
Local time
Yesterday, 18:10
Joined
May 3, 2012
Messages
5
I have have an access 2016 application which send emails via outlook with reports attached as pdf files - all working.

In outlook, my primary account is myemail@talktalk.net

I have now added a second account (gmail) to outlook and wish to use this account to send these emails.

The code in black text has been working.

I have tried adding the lines in red text - one at a time and both.
However, when previewing the email, it is being sent by myemail@talktalk.net, and not the @gmail account.


Dim olApp As Object
Dim objMail As Object
Set olApp = CreateObject("Outlook.Application")
Set objMail = olApp.CreateItem(olMailItem)

With objMail
.To = strRecipients
.SendUsingAccount = "MyGmailAccount@gmail.com"
.Sender = "MyGmailAccount@gmail.com"

.Subject = strSubject
.Body = strBody
.Attachments.Add OutputPathFileAll 'all leagues pdf
.Attachments.Add OutputPathFileCup 'cup pdf
.Attachments.Add OutputPathFileLeague 'notice board per league pdf
If Me.CheckBox_SendWithoutChecking = True Then
.Send 'sends anyway
Else
.Display 'to show the email before sending
End If
End With


Any suggestions most welcome!

John
 
I'm going to point you to where you can do the research for this. As long as you are still on Classic Outlook, this can work for you.


This points to a section in the Outlook Application Object Model. Basically, you can iterate through the accounts available in the application's Accounts collection and select the one you want. You will have to browse through that a bit to see what they are doing, and I can't help you much beyond pointing to this resource as I have never had to play with that. I've sent mail via Outlook Classic and VBA, but never had alternate accounts from which to choose, so I've never actually done this. But this is where I would start looking.
 
Here is how I used to do it. Intaccount is the order of the accounts in Outlook.
Code:
            If rs!ClientDivision = "SSW" Then
                Set objOutlookRecip = .Recipients.Add("Jim Needs - Personal")
                objOutlookRecip.Type = olTo
                intAccount = 2
            Else
'                Set objOutlookRecip = .Recipients.Add("SSAFA West Glamorgan Branch")
                Set objOutlookRecip = .Recipients.Add("South West Wales SSAFA")
                objOutlookRecip.Type = olTo
                intAccount = 3
            End If
then with the outlook message
Code:
            .SendUsingAccount = objOutlook.Session.Accounts.Item(intAccount)
I have code somewhere to determine which account, but travelling atm and in IAH for a flight to Heathrow.

Found it, but this is Outlook VBA, so you will need to adapt it
Code:
Public Function ListEMailAccounts(AcctToUSe As String) As Integer
    Dim outApp As Object
    Dim i As Integer
    Dim AccNo As Integer
    Dim emailToSendTo As String
    
    Set outApp = CreateObject("Outlook.Application")
    'emailToSendTo = ""                    'put required email address
    AccNo = 1
    'if smtp address=email we want to send to, acc no we are looking for is identified
    For i = 1 To outApp.Session.Accounts.Count
        'Uncomment the Debug.Print command to see all email addresses that belongs to you
'Debug.Print "Acc name: " & OutApp.Session.Accounts.Item(i) & " Acc number: " & i & " , email: " & OutApp.Session.Accounts.Item(i).SmtpAddress
        'If OutApp.Session.Accounts.Item(i).SmtpAddress = emailToSendTo Then
        If outApp.Session.Accounts.item(i).DisplayName = AcctToUSe Then

            AccNo = i
            Exit For
        End If
    Next i
    ListEMailAccounts = AccNo
    Set outApp = Nothing
End Function
Just in case the accounts get moved around, or are different on other computers.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom