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:
- 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
- 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.