Excel VBA Send File as Email Attachment: Step-by-Step
In this article
You can use Excel VBA to send a file as an email attachment by automating Outlook. The core method is to create an Outlook mail item, attach the file with the .Attachments.Add method, fill in the recipient, subject, and body, then call .Send or .Display. This works with the desktop version of Outlook on Windows; it does not work with Outlook on the web alone or with the Mac version in the same way, and it requires that Outlook is installed and configured on the same computer.
What You'll Need
- Microsoft Excel (desktop version) and Microsoft Outlook (desktop version) installed on the same Windows PC.
- Outlook set up with a working email account and the ability to send mail.
- Basic familiarity with the VBA editor: press Alt+F11 to open it, and Alt+F11 again to return to Excel.
- Macro security settings that allow macros to run (you may need to enable macros for the workbook, or trust the location).
- The full path to the file you want to attach, for example
C:\Reports\Summary.xlsx. You can also build the path dynamically from the workbook's location.
Step-by-Step
- Open the VBA editor. In Excel, press Alt+F11. In the Project Explorer, right-click your workbook and choose Insert > Module. A blank module appears.
- Add a reference to the Outlook object library (optional but recommended). In the VBA editor, click Tools > References. Scroll down and tick Microsoft Outlook 16.0 Object Library (the version number may differ). Click OK. This enables early binding, which gives you IntelliSense and named constants. If you skip this, use late binding as shown in the next step.
- Write the macro. Paste the following code into the module. It uses late binding so it works without the reference, but you can switch to early binding by changing
ObjecttoOutlook.Applicationand so on. Replace the placeholder file path and email details with your own.Sub SendFileAsAttachment() Dim outlookApp As Object Dim outlookMail As Object Dim filePath As String Dim recipient As String Dim subjectLine As String Dim bodyText As String ' Set the full path to the file you want to attach filePath = "C:\Reports\Summary.xlsx" recipient = "someone@example.com" subjectLine = "Monthly report attached" bodyText = "Please find the report attached." ' Check that the file exists If Dir(filePath) = "" Then MsgBox "File not found: " & filePath, vbExclamation Exit Sub End If ' Create Outlook application and mail item Set outlookApp = CreateObject("Outlook.Application") Set outlookMail = outlookApp.CreateItem(0) ' 0 = olMailItem With outlookMail .To = recipient .Subject = subjectLine .Body = bodyText .Attachments.Add filePath ' Use .Display to review before sending, or .Send to send immediately .Display ' .Send End With ' Release objects Set outlookMail = Nothing Set outlookApp = Nothing End Sub - Adjust the file path. Change
filePathto the actual location of your file. If the file is in the same folder as the workbook, you can useThisWorkbook.Path & "\Summary.xlsx". Make sure the path includes the file extension and uses backslashes on Windows. - Set recipient, subject, and body. Edit the
recipient,subjectLine, andbodyTextvariables. For multiple recipients, separate addresses with semicolons:"a@example.com; b@example.com". - Choose Display or Send. The code above uses
.Displayso you can check the email before sending. To send automatically, comment out.Displayand uncomment.Send. Note that Outlook may show a security warning when sending programmatically; see Troubleshooting. - Run the macro. Press F5 while the cursor is inside the subroutine, or go back to Excel and assign the macro to a button. If Outlook is not already open, it will start. If you used
.Display, an email window appears with the attachment; review and click Send. - Save your workbook as a macro-enabled file. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Otherwise your macro will be lost when you close the file.
Common Mistakes
- Using a relative path or just a filename.
.Attachments.Add "Summary.xlsx"will fail unless the file is in the current working directory. Always supply the full path or build it withThisWorkbook.Path. - Forgetting to check the file exists. Without a check, you get a runtime error 53 (File not found) or a silent failure. The
Dircheck above prevents that. - Leaving Outlook closed and expecting instant send. The code creates an Outlook instance, but if Outlook is not configured or is in a state that blocks automation, it may fail. Start Outlook manually once to ensure it is set up.
- Not handling the Outlook security prompt. Outlook may display a dialog asking whether to allow access. This is normal; see Troubleshooting for options.
- Using early binding without the reference. If you declare
Dim outlookApp As Outlook.Applicationbut did not add the Outlook object library reference, you get a compile error. Either add the reference or use late binding as shown. - Forgetting to release objects. Not setting objects to
Nothingcan leave Outlook processes running in the background. Always clean up.
Tips
- Use
.Displayduring development so you can inspect the email before anything is sent; switch to.Sendonly when you are confident. - To attach multiple files, call
.Attachments.Addonce per file with a full path each time. - If you need to send to a contact group or use BCC, set
.BCCor.CCproperties similarly. - Store the file path in a cell or a named range so non-technical users can change it without editing code.
- Consider adding error handling with
On Error GoToto show a friendly message if Outlook is unavailable. - Test with your own address first to avoid accidental sends to real recipients.
Troubleshooting
If the macro does nothing or shows a permission error: Outlook's Trust Center may block programmatic access. In Outlook, go to File > Options > Trust Center > Trust Center Settings > Programmatic Access and check the setting. If it is set to warn, you may see a prompt each time; you can choose to allow for a period. If it is set to deny, change it to warn (administrator rights may be needed).
If you get "File not found": Double-check the full path, including the file extension. Use Dir(filePath) in the Immediate Window (Ctrl+G) to test. Ensure the file is not open in a way that locks it.
If Outlook does not start: Make sure Outlook is installed and configured with at least one email account. Try opening Outlook manually first. If Outlook is already running but hidden, the automation may attach to the existing instance; close all Outlook windows and retry.
If the email sends but the attachment is missing: Verify that .Attachments.Add is called before .Send and that the path is correct. Check the Sent Items folder to confirm.
If you get a compile error about user-defined type: You are using early binding without the Outlook reference. Either add the reference (Tools > References) or switch to late binding by declaring objects as Object and using CreateObject.
If the macro is slow or leaves Outlook processes running: Ensure you set objects to Nothing and avoid creating multiple Outlook instances. If needed, use GetObject to reuse an existing instance.
Frequently Asked Questions
Can I send an email without Outlook installed?
No, this method relies on Outlook's automation interface. Alternatives include using CDO or a third-party library, but those require different setup and are not covered here.
Does this work on Mac?
VBA in Excel for Mac can automate Outlook for Mac to some extent, but the object model and behavior differ, and some methods may not work. The steps above are for Windows.
How do I attach a file from a network drive?
Use the full UNC path, for example \\server\share\file.xlsx. Ensure the drive is accessible and you have permission.
Can I send to multiple recipients?
Yes, separate addresses with semicolons in the .To property, or use .CC and .BCC.
Why does Outlook show a security warning?
Outlook protects against malicious code that sends email without consent. The warning appears when a program tries to access address books or send email. You can adjust Trust Center settings, but be aware of the security implications.
Can I include the workbook itself as an attachment?
Yes, use ThisWorkbook.FullName as the file path. However, if the workbook has unsaved changes, attach a saved copy or save first.