Outlook 2013 and up have an option to always send new messages using the default email account.
Macro using the Default Account
An IMAP user created this macro to open a new message form from any message store and use the default account as assigned in Account Settings.
To use: Go to File, Options, Customize Ribbon. Select Macros from Choose Commands from dropdown, add a New Group to Home tab then add the New Mail macro to the new group. Click the Rename button to rename the command and choose a better looking icon.
Text file containing both macros on this page.
Public Sub New_Mail() Dim olNS As Outlook.NameSpace Dim oMail As Outlook.MailItem Set olNS = Application.GetNamespace("MAPI") Set oMail = Application.CreateItem(olMailItem) 'use first account in list oMail.SendUsingAccount = olNS.Accounts.Item(1) oMail.Display Set oMail = Nothing Set olNS = Nothing End Sub
See Using VBA Codeif you need help using VBA code.
Macro using a specific account
This macro is assigned to a button on the ribbon - clicking the button selects the account listed in the code. Replace Name_of_Default_Account with your account name (check in Account settings for the account name). You can create a macro for each account (change the Public Sub name) if desired.
Go to File, Options, Customize Ribbon. Select Macros from Choose Commands from dropdown, add a New Group to Home tab then add the New Mail macro to the new group. Click the Rename button to rename the command and choose a better looking icon.
If you need help customizing the ribbon, see Customizing the Quick Access Toolbar (QAT)
See Using VBA Code if you need help using VBA code.
Text file containing both macros on this page.
Public Sub New_Mail() Dim oAccount As Outlook.Account Dim oMail As Outlook.MailItem For Each oAccount In Application.Session.Accounts If oAccount = "Name_of_Default_Account" Then Set oMail = Application.CreateItem(olMailItem) oMail.SendUsingAccount = oAccount oMail.Display End If Next End Sub
Note: this macro also works with Outlook 2007 (possibly older versions).
Set the From address on a message
Similar to the previous macro, except it uses a different From address, not a different email account in your profile. Because the message is sent from the default email account, the default account needs Send As permission for the address.
If the account does not have Send as permission, the message will be sent from the default account on behalf of the address or will bounce if using Exchange server. If you are using a SMTP server the message may be sent from the default account.
Use this code to fill in the From field with an address you have permission to send messages From.
Public Sub CreateNewMessageFrom() Dim objMsg As MailItem Set objMsg = Application.CreateItem(olMailItem) With objMsg .SentOnBehalfOfName = "alias@domain.com" .BCC = "alias2@domain.com" .Display End With Set objMsg = Nothing End Sub
Send a meeting request using a specific account
Use this macro to send meeting requests using a specific account, irregardless of which data file you are viewing.
Public Sub NewMeeting() Dim oAccount As Outlook.Account Dim oMeeting As Outlook.AppointmentItem For Each oAccount In Application.Session.Accounts If oAccount = "account@displayname" Then Set oMeeting = Application.CreateItem(olAppointmentItem) oMeeting.MeetingStatus = Outlook.OlMeetingStatus.olMeeting oMeeting.SendUsingAccount = oAccount oMeeting.Display End If Next End Sub
Using VBA Code
To use VBA code, press Alt+11 to open the VBA Editor. Locate ThisOutlookSession and paste the code into the editor.
Press F5 or the Run button to test the macro. (It's highly recommended you make a backup of the folder or message store before running macros.)
You'll also need to change macros security or use selfcert.exe to sign your macros. Access the dialog to change the security level from Tools, Macros, Security. (This is at File tab, Options, Trust Center, Macro Security in Outlook 2010). Set it on "always ask". Do not choose the Low option (run all, never ask). Some security software will set it to High and your macros will not run.
To run a macro later, press Alt+F8 to open the macro dialog, then select the macro and choose Run. Or add a button for the macro to your toolbar or add it to the QAT in Outlook 2010.
More Information
How to Enable the Developer Ribbon
See How to use VBA code samples in Outlook for more detailed information on using macros and selfcert.
Kudo's to you Diane !! Normally I get my "help" on stackoverflow but for my current job to automate sending an email from excel your articles and comments gave me all the help to do the job. thank u !!
Hi Diane, is there a way we can specify the script to only run when e-mailing from a specific account. I have business and personal accounts in this case I would like the script only to send on behalf of when replying from my business account user@domain.com
You can use an If statement to either skip or send
For Each oAccount In Application.Session.Accounts
If oAccount = "business@account" Then
'do whatever
End if
next
Thank you for the response. As I do not have much experience with coding could you show me where in this code I would add the proper statements. Also would it be possible to put a universal variable such as if oAccount source is *@xyz.com then set sender address to delegate@domain.com:
Dim WithEvents objInspectors As Outlook.Inspectors
Dim WithEvents objMailItem As Outlook.MailItem
Dim WithEvents myOlExp As Outlook.Explorer
Private Sub Application_Startup()
Initialize_handler
End Sub
Public Sub Initialize_handler()
Set objInspectors = Application.Inspectors
Set myOlExp = Application.ActiveExplorer
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then
Set objMailItem = Inspector.CurrentItem
If objMailItem.Sent = False Then
Call SetFromAddress(objMailItem)
End If
End If
End Sub
'Uncomment the next 3 lines to enable Outlook 2013/2016/2019/365 Reading Pane Reply
'Private Sub myOlExp_InlineResponse(ByVal objItem As Object)
' Call SetFromAddress(objItem)
'End Sub
Public Sub SetFromAddress(oMail As Outlook.MailItem)
' Set your preferred default From address below.
' Exchange permissions determine if it is actually stamped
' as "Sent On Behalf Of" or "Sent As".
' The address is not properly updated for the InlineResponse
' feature in Outlook 2013/2016/365. This is only a visual bug.
oMail.SentOnBehalfOfName = "delegate@domain.com"
End Sub
I have this code that I use with a ribbon button:
Sub FORWESP()
Dim objMail, objForward As Outlook.MailItem
Dim BodyIni, BodyEnd As String
Set objMail = Outlook.Application.ActiveExplorer.Selection.Item(1)
Set objForward = objMail.Forward
BodyIni = objForward.Body
BodyEnd = "Dear Xxxx," & vbCrLf & vbCrLf & "blabliblabli blabli" & vbCrLf & vbCrLf & "Sincerely" & vbCrLf & BodyIni
With objForward
.Display
.Subject = "FW: " & objMail.Attachments.Item(1).FileName
.Body = BodyEnd
End With
End Sub
and now i want to also use a specific outlook account to send the email and I took this code from this page:
Public Sub New_Mail()
Dim oAccount As Outlook.Account
Dim oMail As Outlook.MailItem
For Each oAccount In Application.Session.Accounts
If oAccount = "x@xxx.com" Then
Set oMail = Application.CreateItem(olMailItem)
oMail.SendUsingAccount = oAccount
oMail.Display
End If
Next
End Sub
But I am not smart enough to combine the two. Must be easy for someone who actually understands code... Would be great if someone could help...
Many Thanks
Josh
This works here -
Sub FORWESP()
Dim objMail, objForward As Outlook.MailItem
Dim BodyIni, BodyEnd As String
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount = "dianep@outlook-marketplace.com" Then
Set objMail = Outlook.Application.ActiveExplorer.Selection.Item(1)
Set objForward = objMail.Forward
objForward.SendUsingAccount = oAccount
End If
Next
BodyIni = objForward.Body
BodyEnd = "Dear Xxxx," & vbCrLf & vbCrLf & "blabliblabli blabli" & vbCrLf & vbCrLf & "Sincerely" & vbCrLf & BodyIni
With objForward
.Display
.Subject = "FW: " & objMail.Attachments.Item(1).FileName
.Body = BodyEnd
End With
End Sub
Hi Diane: can you tell me why some vba scripts "MakeItem" in their code and others use "msg.display" to view email templates.
Mailitem is used when you are opening a new message, .display displays the message.
Hi. RE: Macro for prepopulating a Subject within a current email (not a new one) How do I get the macro to process within the email I am currently in instead of it populating a new one email altogether as soon as I engage the macro? I use a system client database called Applied EPIC and it works with outlook. I click on my client's email address within that database and it activates an outlook email window (EPIC is connected to Outlook). I hit ALT N→ AS to drop a prepopulated signature and then ALT N→Y to engage a macro (which I built to prepopulate the subject). I already tried Quick Steps, and it is the same (only starts a new email when i want it to use the one I am in). Here is the Macro Sub New_Secure_Email() Dim MItem As MailItem Set MItem = Application.CreateItem(olMailItem) MItem.subject = "friendly reminder; renewal 10/9/20" MItem.Display End Sub Can i recode the macro so that it will work within the email i am in? When i use this one it pops up a *new email* which i cannot use because i need to use the one that happened in the beginning (because… Read more »
This line calls for a new message:
Set MItem = Application.CreateItem(olMailItem)
You need to use activeinspector
Set MItem = Application.ActiveInspector.CurrentItem
Thanks... that makes sense. HOWEVER, now i get an error message "The macros in this project are disabled. Please refer to the online help or documentation of the host application to determine how to enable macros." I wasn't getting this message before. It worked (albeit, creating a new one.). Thoughts?
Are macros enabled in file > options > trust center > macro security?
nevermind. I had changed back the set mltem by mistake and now using the one you suggested. IT's WORKING NOW.
Sub New_Secure_Email()
Dim MItem As MailItem
Set MItem = Application.ActiveInspector.CurrentItem
MItem.subject = "friendly reminder; renewal 10/12/20"
MItem.Display
End Sub
Hi,
Some great stuff and pretty easy to follow . But I#m not sure I can make the next leap :-(
As part of a mail rule script I would like to forward an email having inserted some text and a picture (in line not as an attachment) before the email that triggered the rule (so just like a normal forwarding process.)
How can I do this with VBA?
TFAI
Hello,
Could anyone direct a novice office user to assist in the following, I think it would be simple and functionality exist.
What I am trying to do is the following:
1) Report a Suspicious Email
2) On a email that has arrived I would like to set a button or method to
a) On selected email, forward to a mail box and then delete the original email.
b) Prefer to attach original email for header inspection.
Based on this we can review the original suspicious email and review prior to sending back to user. More for Phishing response due to amount of volume.
Basically I can view a inbox with suspicious email for review, with original attachment can inspect headers and URL in sandbox environment for inspection.
Dont expect it to be unique requirement so possibly a Macro or VSB script exists.
Can I use powershell as more experience on this tool than VBA ?
A macro can do it, not sure about powershell. i have this sample for spam reports - it includes the message header in the forward body.
https://www.slipstick.com/developer/code-samples/outlooks-internet-headers/#spam
Wow, why is this so hard? What I need is to make sure that Mailmerge from Word uses my default account (gmail) rather than my hotmail account.
gmail is default for email and Hotmail is the default data file? The non-macro method is to make second profile with only the gmail account.