Email created from Excel with VBA doesn't send from intended email address

Anonymous
2024-02-05T09:31:07+00:00

Hi Microsoft Community.

I have VBA code which creates an email in Outlook from Excel.

It all works successfully, but I cannot change the email address I'm sending from.

I have 3 different email accounts in Outlook. This code always sends from what was the default email account, which I've changed (send from this account by default) since I started working on this VBA code. It still doesn't send from the account I want to use.

I've read quite a few posts from various sites, but alas, I cannot understand them.

The attached screenshot shows the row I'm trying to get to change the sender's email address, and the error message that shows. It refers to the line:

"EmailItem.SendUsingAccount = "******@domain.com"

What am I doing wrong? Is there a simple way to do what I want?

Thanks for your help.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-02-05T12:42:48+00:00

    Hi Ivan

    I'm AnnaThomas and I'd happily help you with your question. In this Forum, we are Microsoft consumers just like yourself no one here is a Microsoft employee.

    Here’s an example of how you can modify your code:

    Sub SendEmail() Dim OutApp As Outlook.Application Dim OutMail As Outlook.MailItem Dim OutAccount As Outlook.Account

    Set OutApp = New Outlook.Application Set OutMail = OutApp.CreateItem(olMailItem)

    ' Loop through the accounts and find the one to send from For Each OutAccount In OutApp.Session.Accounts If OutAccount.SmtpAddress = "*********************" Then Set OutMail.SendUsingAccount = OutAccount Exit For End If Next

    With OutMail . Subject = "Test" . Body = "Test Body" . Recipients.Add ("********************") . Send End With

    Set OutMail = Nothing Set OutApp = Nothing End Sub

    I hope this helps ;-), let me know if this is contrary to what you need, I would still be helpful to answer more of your questions.

    Best Regards,

    AnnaThomas

    Give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-02-06T15:17:22+00:00

    Thanks AnnaThomas.

    I ran the code, then substituted my own email address and email account of the email account I wish to send from.

    When it gets to 2 lines above the highlighted line (For Each OutAccount. In ...) OutAccount shows the name of the account (Ivan ***ail, etc.) on each Next.

    The next line OutAccount.smtpAddress shows the email address (ivan....@gmail, etc.) of each Account on each Next.

    Before it gets to the highlighted line, OutMail.SendUsingAccount = Nothing, of course, because it hasn't run yet.

    When it tries to run the highlighted line (Set Outmail.SendUsingAccount = "myemail" it gives Runtime Error 91.

    So far, when it executes all the code, it produces an email but always with the wrong sending email account.

    Any ideas?

    Cheers,

    Ivan

    0 comments No comments