Do you often need to attach specific files when sending specific emails in Outlook? If so, you may feel it tedious to add them manually every times. This article will teach you a quick method to achieve it with Outlook VBA.
In my daily works, I am frequently required to attach specific files when sending specific emails. For instance, every week, I have to send an email to my superiors so as to report my weekly work. In this email, I will need to attach some specific files, including work logs, some data statistic reports and so on. I usually update these files every week. In the past, I normally first compose the message body and then manually add these files. However, at present, I find a smarter trick. That is using Outlook VBA to automate it. If you have the similar requirements, please read on to learn the concrete VBA codes and the detailed operations.
Auto Attach Specific Files When Sending Specific Emails
- For a start, launch Outlook and turn to “Developer” tab.
- Then you can find and click on the “Visual Basic” button, which will bring out the VBA editor window at once.
- In the subsequent window, you should double click the “ThisOutlookSession” project on the left side project list.
- Next you ought to copy and paste the following VBA codes into it.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim strPrompt As String Dim nRes As Integer 'Check the email subject and recipient If InStr(LCase(Item.Subject), "worklog") > 0 And InStr(Item.To, "frank") > 0 Then strPrompt = "Do you want to attach the worklog related files?" nRes = MsgBox(strPrompt, vbYesNo + vbQuestion, "Attach Files") If nRes = vbYes Then 'Attach the specific files With Item .Attachments.Add ("C:\Attachments\WorkLog.docx") .Attachments.Add ("C:\Attachments\Data Statistics Report 1.xlsx") .Attachments.Add ("C:\Attachments\Data Statistics Report 2.xlsx") End With End If End If End Sub
- After that, you’ll be required to assign a digital certificate to the new project.
- Firstly click “Tool” button in toolbar and choose “Digital Signature”.
- Then in the popup “Digital Signature” dialog box, hit “Choose” button,
- Subsequently, you can choose an existing certificate.
- Lastly click a series of “OK” to assign it successfully.
- Later you have to modify the macro settings to simply allow digitally signed macros.
- Eventually you can have a try.
- When you click “Send” to send the specific mail that contains the specific words in subject or is sent to specific contact, you will see a message box, like the image below.
- After that, you can check the sent email. You will surely find the preset files attached in the sent mail.
Never Defer Fixing Outlook Errors
Sometimes, even though you encountered an error message in Outlook, after you close the message, you may still be able to keep working. But actually it is not an advisable action. What you should do is to eliminate them as soon as possible instead of leaving them aside. Accumulating issues will result in serious crash. If your Outlook crashes, your Outlook PST data will be put at risk. You should know that corrupted Outlook PST data is rather knotty. At that time, you have no choice but to use an experienced and reputable repair tool, such as DataNumen Outlook Repair.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair damaged SQL Server file and outlook repair software products. For more information visit www.datanumen.com