Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Send email through VBA from shared email box

0

Hello,

I followed the instructions from the "Practical Tasks - Import, Export & Email Ultimate Guide > Email from Excel (The Full Guide) - With Multiple Recipients, Attachments, HTML, Signatures, Email Error Handling & More" so that I can send an email with attachments using VBA. The guide worked perfectly; however, I am trying to send the email from a second shared email box that I have access to. As of right now the code will send from my box and I am looking to have it sent from the second email box.

When I manually open an email I can select to send from my email or the shared box. How can I adjust the coding to send from this second box?

We use MAPI to send emails

Answer
Discuss

Answers

0
Selected Answer

Hi Mbodine and welcome to the Forum.

Thanks for changing your question (to remove code from a Premium Course). Here's my... Revised Answer #1 13 August 2024:

Given you're using MAPI, the code in the attached file's Module 1 should work (for POP3 too), provided you:

  1. have the reference (mentioned at the start of the code below) enabled, e.g. Microsoft Outlook 14.0 Object Library as in the file and
  2. change the bits in bold, especially "firstname.lastname@provider.com" which MUST be changed for one of the accounts used in your email app:
Sub SendFromStatedAccount()

    ' needs "Microsoft Outlook <<Version>> Object Library" (find/enable in VBA Tool/References)

    Dim outlookApp As Outlook.Application
    Dim emailItem As Outlook.MailItem
    Dim outlookAccount As Outlook.Account

    On Error Resume Next

    Set outlookApp = CreateObject("Outlook.Application") 'get Outlook

    ' CHANGE to a valid sending account...
    Set outlookAccount = outlookApp.Session.Accounts("firstname.lastname@provider.com")

    ' create email
    Set emailItem = outlookApp.CreateItem(olMailItem)
    ' populate email
    With emailItem
        .To = "email1@example.com"
        .CC = ""
        .BCC = ""
        .Subject = "Email from stated account"
        .Body = "Hello"
        .SendUsingAccount = outlookAccount
        .Display
        '.Send
    End With

    Set outlookAccount = Nothing

End Sub

When you run this, it should display an email from whichever account you specify in the code (and you can press Send to check).

Hope this fixes your problem. If so, please be sure to mark this Answer as Selected.

Discuss

Discussion

I removed the code - I apologize for that!

Also, I am using MPI to send emails.
Mbodine (rep: 2) Aug 13, '24 at 12:55 pm
Thanks for that. I'll reply a bit later (by revising my Answer).
John_Ru (rep: 6377) Aug 13, '24 at 1:48 pm
Please see my revised Answer and file.
John_Ru (rep: 6377) Aug 13, '24 at 2:54 pm
Sounds like that worked for you, Mbodine. Thanks for selecting my Answer.
John_Ru (rep: 6377) Aug 13, '24 at 4:32 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login