0 items - $0.00

Your shopping cart is empty

Why not add some items in our Shop
Call Us at +27 21 786 1224
Home /

Excel VBA

My mentor David Wood from AdvancedApps has helped me out with code on how to send email from an Outlook Account where this Account is not the default Account.

Outlook 2016 - Select the account from where email must be send.

I am trying to send mail from a specific account. This account in Outlook is not the default account. In this case what makes it much easier is that the user can select the email address rather than account number. sendCaller loops through the accounts until it finds this email address. From there on it will call sendFile from where the message will be send.

[CODE]Sub sendCaller()
'creates outlook application
'chooses an email address and finds the corresponding account number

Dim OutApp As Object
Dim i As Integer, accNo As Integer

Set OutApp = CreateObject("Outlook.Application")
emailToSendTo = "name@domain.com" 'put required email address

'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 accNo = i
Next i

sendFile accNo

End Sub[/CODE]

[CODE]Sub sendFile(accountNo As Integer)
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail

.To = "recipient@domain.com"
.Subject = "Test"
.Body = "Body"
Set .SendUsingAccount = OutApp.Session.Accounts.Item(accountNo)
End With
End Sub[/CODE]