Excel VBA - Send email in Outlook using 2nd account

trimShady

New Member
Joined
Jan 17, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm new to VBA. I have a working macro below to send Outlook mail using Excel VBA:

VBA Code:
Sub Use_2nd_Account()

Dim outlook As outlook.Application
Dim outlookmail As outlook.mailItem

Set outlook = New outlook.Application
Set outlookmail = outlook.CreateItem(olMailItem)

With outlookmail
    .To = Sheet1.Cells(2, 1).Value
    .CC = ""
    .Subject = Sheet1.Cells(2, 2).Value
    .Attachments.Add ActiveWorkbook.path & "/attachment1.png"

    .HTMLBody = "hello world!" 
     
     'Set .SendUsingAccount = .Session.Accounts.Item(2)  
    
    .Display
    
End With

End Sub

I'd like to be able to send email using a 2nd account but I am having trouble getting the "Set .SendUsingAccount" to work. I'm using Office365. Any help would be greatly appreciated. Thanks in advance.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I use this method successfully. I don't know if it's the best, but it works. I do this in Outlook, have not tested in Excel.

By the way, it's not a good practice to use "outlook" as a variable name.
VBA Code:
Sub Use_2nd_Account()

Dim outlook As outlook.Application 
Dim outlookmail As outlook.mailItem

Set outlook = New outlook.Application

Dim oAccount As Outlook.Account

For Each oAccount In outlook.Session.Accounts
   If oAccount = "name_of_account" Then ' the account name is usually an email address
      Set outlookmail = outlook.CreateItem(olMailItem)
      outlookmail.SendUsingAccount = oAccount
      Exit For
   End If
Next


With outlookmail
    .To = Sheet1.Cells(2, 1).Value
    .CC = ""
    .Subject = Sheet1.Cells(2, 2).Value
    .Attachments.Add ActiveWorkbook.path & "/attachment1.png"

    .HTMLBody = "hello world!"
    
     'Set .SendUsingAccount = .Session.Accounts.Item(2) 
   
    .Display
   
End With

End Sub
 
Upvote 0
Thank you for the response. I don't know how to create/use VBA in Outlook so this is not gonna help me.
I use this method successfully. I don't know if it's the best, but it works. I do this in Outlook, have not tested in Excel.


I'll keep this in mind going forward.
By the way, it's not a good practice to use "outlook" as a variable name.


Thanks again.
 
Upvote 0
Thank you for the response. I don't know how to create/use VBA in Outlook so this is not gonna help me.
Of course it's gonna help you. I just extended your code using the same technique I use in Outlook. If your original code works in Excel, then my modifications will work in Excel.
 
Upvote 0
I ran your code in Excel and got...
Run-time error '91':
Object variable or With block variable not set

When I click debug, it highlights
VBA Code:
.To = Sheet1.Cells(2, 1).Value

When I comment out your code, it runs fine. I even hardcoded the email like so...

VBA Code:
.To = "test@test.com"

But getting the same error with your modification.
 
Upvote 0
I got it working. I neglected to replace the "name_of_account" from your modification with the actual 2nd account email. Once I did that, your code worked. Thank you so much. ❤️
 
Upvote 0
Glad it worked! I should have emphasized the tailoring needed more prominently.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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