Selecting Outlook mail account in VBA macro generated mail from Excel

Rustum

New Member
Joined
Mar 22, 2011
Messages
2
I am using a VBA macro in Excel 2007 to send emails from Outlook 2007 using info in the excel sheet. I wanted to be able to send from a different mail account than the default and added the following line to the macro before the .send command
.SendUsingAccount = OutApp.Session.Accounts.Item(3)
but it has no effect. Whatever account number is selected as the Item Outlook still sends from the default account. As a 'workaround' I can change the default account before I run the macro but I would prefer to have it select the correct account from within the macro. Does anyone know what I may be doing wrong or know a way to do this?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

I have been using Ron de Bruin's code to generate the emails from Excel and the two examples of selecting an account of his you have linked to through the other post work fine for me from Outlook. The problem I have seems to lie in getting the account selection operating from an Excel as opposed to an Outlook macro. I have been careful to add in the Excel VBA editor a reference to the Outlook Object Library but I guess I still have something wrong. Perhaps failed to make the appropriate declarations? Just don't know
 
Upvote 0
Hi,

the example given by Ron works for me as posted below - except you need to add a valid email address and select account item number
Here it is copied from Use the Account you want in mail macro in Excel/Outlook 2007-2010

I added a gmail account to my Outlook as it only had one account and then added the reference to Microsoft Outlook 12.0 Object Library and sent the mail using item(2).
Outlook stated sending using gmail accout

I can only suggest trying Rons simple example first and then move on to yours if it works.

Code:
Sub Mail_small_Text_Change_Account()
'Is only working in Office 2007-2010
'You must add a reference to the Microsoft Outlook Library
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "[EMAIL="me@gmail.com"]me@gmail.com[/EMAIL]"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'SendUsingAccount is new in Outlook 2007
'Change Item(1)to another number to use another account
.SendUsingAccount = OutApp.Session.Accounts.Item(2)
.Send

End With

On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top