If you have several email accounts configured in your Outlook, sometimes you may want to use specific email accounts to reply or forward specific emails. This article will teach you how to automate it with Outlook VBA.
The previous article in our blog – “How to Send Emails from a Specific Account in Your Outlook”, has introduced a method to always use a specific email account for sending emails. But by that means, Outlook will send out all the emails using the specific account. That is to say, you cannot specify different accounts for sending different emails, for instance, using one account when forwarding specific emails and using another when replying specific emails. Thus, in response to this issue, here we will teach you a smart and handy approach, which will use VBA. Read on to get it in detail.
Auto Use Different Accounts Based on Whether Replying or Forwarding Specific Emails
- For a start, launch you Outlook properly.
- Then press “Alt + F11” key buttons in Outlook main window.
- Next you will enter the VBA editor.
- Subsequently, you need to double click on the “ThisOutlookSession” project on the left side.
- After that, you ought to coy the following VBA codes into this project.
Public WithEvents objExplorer As Outlook.Explorer Public WithEvents objMail As Outlook.MailItem Public objAccounts As Outlook.Accounts Public objAccount As Outlook.Account Private Sub Application_Startup() Set objExplorer = Outlook.Application.ActiveExplorer Set objAccounts = Outlook.Application.Session.Accounts End Sub Private Sub objExplorer_SelectionChange() On Error Resume Next Set objMail = objExplorer.Selection.Item(1) End Sub Private Sub objMail_Reply(ByVal Response As Object, Cancel As Boolean) Dim objReply As Outlook.MailItem 'Get the specific email 'You can change the condition as per your needs If InStr(LCase(objMail.Subject), "datanumen") > 0 Then 'Use the specific email account to reply For Each objAccount In objAccounts If objAccount.DisplayName = "firstname.lastname@example.org" Then Set objReply = objMail.Reply objReply.SendUsingAccount = objAccount objReply.Display End If Next Cancel = True End If End Sub Private Sub objMail_Forward(ByVal Forward As Object, Cancel As Boolean) Dim objForward As Outlook.MailItem 'Get the specific email 'You can change the condition as per your needs If objMail.SenderEmailAddress = "email@example.com" Then 'Use the specific email account to forward For Each objAccount In objAccounts If objAccount.DisplayName = "firstname.lastname@example.org" Then Set objForward = objMail.Forward objForward.SendUsingAccount = objAccount objForward.Display End If Next Cancel = True End If End Sub
- Later you need to sign this code.
- Then change your Outlook macro settings to allow digitally signed macros.
- Eventually you can restart Outlook to activate this VBA project. From now on, Outlook will automatically use the specific email accounts when you reply or forward specific emails.
Keep Prepared for PST Troubles
As Outlook is vulnerable, your PST file is also prone to corruption. Therefore, you have to keep well-prepared for all potential dangers, such as corrupt PST data. So, what you must do is to make consistent and regular backups for Outlook data. In addition, it is prudent to get hold of a potent repair tool, like DataNumen Outlook Repair.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including sql recovery and outlook repair software products. For more information visit www.datanumen.com