VBA Code to Send an Email With an Attachment

VBA & macros · Updated November 18, 2026

To send an email with an attachment from Excel VBA on Windows, create an Outlook mail item, add the file with .Attachments.Add, and call .Send. The macro below uses classic Outlook; it checks that the file exists and displays the message for review before sending, so you can safely verify the recipient and attachment first.

Sub SendEmailWithAttachment()
    Dim olApp As Object
    Dim olMail As Object
    Dim attachmentPath As String

    ' Replace this with the full path to the file you want to attach.
    attachmentPath = "C:\Reports\monthly-report.xlsx"

    If Len(Dir$(attachmentPath)) = 0 Then
        MsgBox "Attachment file not found: " & attachmentPath, vbExclamation
        Exit Sub
    End If

    On Error GoTo HandleError

    ' Connect to Outlook if it is open; otherwise start it.
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    On Error GoTo HandleError
    If olApp Is Nothing Then Set olApp = CreateObject("Outlook.Application")

    Set olMail = olApp.CreateItem(0) ' 0 is an Outlook mail item.

    With olMail
        .To = "recipient@example.com"
        .CC = ""
        .Subject = "Monthly report"
        .Body = "Hello," & vbCrLf & vbCrLf & _
                "Please see the attached report." & vbCrLf & vbCrLf & _
                "Regards"
        .Attachments.Add attachmentPath
        .Display ' Review the draft. Replace with .Send to send immediately.
    End With

CleanUp:
    Set olMail = Nothing
    Set olApp = Nothing
    Exit Sub

HandleError:
    MsgBox "The email could not be created: " & Err.Description, vbExclamation
    Resume CleanUp
End Sub

Change the path, email address, subject, and message to suit your task. When you are ready to send without opening a draft, replace .Display with .Send. Save a copy of your workbook before running any macro: VBA actions cannot be undone with Ctrl+Z.

What this macro requires

This example is intended for Excel for Microsoft 365 on Windows with classic Outlook installed and configured with an account. It uses Outlook’s COM automation interface, which lets VBA create a message and attach a local file. It does not send mail by itself: Outlook provides the mail account and handles delivery.

The newer Outlook for Windows does not support this traditional COM automation method. If your organization has moved you to new Outlook, the macro may fail to create an Outlook application object. Use classic Outlook for this specific approach, or ask your IT team about an approved alternative such as Microsoft Graph or Power Automate. Excel for the web cannot run VBA macros. The Windows code above also does not work unchanged in Excel for Mac, where Outlook automation uses different mechanisms.

How to put the code in Excel

  1. In Excel, save a working copy of the file. To preserve the macro, save it as an Excel Macro-Enabled Workbook (.xlsm).
  2. Open the Visual Basic Editor with Alt+F11 on Windows. If the Developer tab is visible, you can also choose Developer > Visual Basic.
  3. In the editor, choose Insert > Module, then paste the procedure into the module.
  4. Replace the sample file path and recipient address. Check that the file path points to an existing file and that the intended account is available in Outlook.
  5. Return to Excel, click inside the procedure in the Visual Basic Editor, and press F5 to run it. The draft should open in Outlook for review.

If the Developer tab is hidden, enable it through File > Options > Customize Ribbon and select Developer. On Mac, the Visual Basic Editor shortcut differs by keyboard and Excel setup; the Windows COM code here is not a Mac-compatible substitute. Macro security settings may also prevent a workbook’s code from running. Only enable macros in files you trust.

How the Outlook VBA code works

The procedure first stores the attachment’s full path in attachmentPath. Dir$ checks whether a file exists at that path; if it does not, the macro shows a message and exits before creating a draft. A full path such as C:\Reports\monthly-report.xlsx is more dependable than a filename alone, because the current working folder may not be the folder containing the workbook.

GetObject tries to connect to an Outlook instance that is already running. If none is available, CreateObject asks Windows to start classic Outlook. The code uses late binding: the Outlook objects are declared as Object, so you do not need to select an Outlook library reference in the VBA editor. The numeric argument 0 passed to CreateItem means a standard mail item.

Inside the With block, the macro sets the recipient, subject, and plain-text body, then adds the file as an attachment. .Display opens the completed message but leaves the final send action to you. The error handler reports a problem, such as Outlook not being available, and the cleanup section releases the VBA references. It does not close Outlook.

Display a draft or send automatically

Using .Display is usually the better starting point. It gives you a chance to catch a mistyped address, stale report, missing attachment, or unintended message before anything leaves your mailbox. After the process is verified, changing that line to .Send submits the item through Outlook without showing the draft first.

Automatic sending is not a guarantee of silent delivery. Outlook configuration, organizational security policies, account state, and application prompts can affect automation. The macro also does not confirm that the recipient received or read the email. For recurring or business-critical mail, include appropriate error logging and a way to review what was sent rather than treating a successful VBA call as proof of delivery.

The sample leaves .CC blank. To add a copy recipient, put an address in that property; for a hidden copy, use the Outlook mail item’s .BCC property. Separate multiple addresses with semicolons. The .To property can likewise contain more than one address, but test the formatting in a displayed draft before automating a batch.

Choosing and attaching files safely

The attachment argument must identify a file that Outlook can access. A local drive path or a mapped network drive may work if the user running Excel has permission to read it. A cloud-synced file should be available locally or otherwise accessible to Outlook at the time the macro runs; a text URL is not automatically treated as a local attachment.

For a different file, change the value of attachmentPath. To attach the workbook containing the macro, for example, a typical path expression is ThisWorkbook.FullName; use it only when the workbook has been saved, because an unsaved workbook may not have a usable filename. To attach the active workbook instead, ActiveWorkbook.FullName refers to whichever workbook is active when the code runs, which may not be the intended one.

Before sending sensitive or large files, consider who can access the destination mailbox, whether the attachment is the correct version, and whether your organization permits sending that information. An attachment can be forwarded or retained beyond your control. If the file is too large for email or contains confidential data, an approved access-controlled sharing link may be more appropriate.

Common errors and practical fixes

  • File not found: Check spelling, extension, folder, and drive letter. Confirm the file is available to the Windows account running Excel. A cloud placeholder may not yet be downloaded.
  • ActiveX component cannot create object: Check that classic Outlook is installed and configured, and that you are not using new Outlook, Excel for the web, or a Mac with this Windows-specific code.
  • Outlook opens but no message appears: Look for an existing Outlook window or a prompt behind Excel. Confirm that execution reached .Display and that no earlier error stopped the macro.
  • Message appears but will not send: Check Outlook’s connection, account configuration, required sign-in, and any organization policy. A message in the Outbox may still be waiting to send.
  • Macro is blocked: Review Excel’s macro security settings and the workbook’s file source. Do not lower security broadly or enable code from an unknown sender just to make a macro run.

When troubleshooting, leave .Display in place and test with your own address and a harmless file. This separates problems creating the message from delivery issues and prevents a debugging run from sending an unintended email.

Plain-text messages, HTML, and multiple attachments

The .Body property in the example creates plain text. The vbCrLf constants insert line breaks; concatenating strings with & combines the parts into one message. For formatted HTML, use the mail item’s .HTMLBody property instead of .Body, and supply valid HTML such as paragraph tags. Do not set both properties expecting them to merge: choose the format you want and review the displayed draft, especially if the content includes data from worksheet cells.

To attach more than one file, call .Attachments.Add once for each full path. Check each path before adding it; a reliable batch process should report which file is missing and avoid sending a partially assembled message. If attachment names or message text come from worksheet cells, validate those values and handle blank rows deliberately. The basic macro is for one specified message, not a bulk-mail system.

Alternatives to Outlook automation

Outlook VBA is convenient when a person is working in Excel on Windows and needs a draft built in their configured classic Outlook account. It is less suitable for unattended, high-volume, or cross-platform sending: Outlook must be available, automation behavior can depend on policy, and errors need to be handled carefully.

For an organization-managed process, Power Automate may be a better fit when the relevant connectors, permissions, and licensing are available. Developers building a service or supported application may consider Microsoft Graph, which requires appropriate identity and mail permissions. SMTP is another possible route only when a permitted mail server and authentication method are available; do not assume a consumer or workplace account allows arbitrary SMTP access. Choose an option approved by your organization rather than storing passwords in a workbook.

Frequently Asked Questions

Can VBA send an email without opening Outlook?
Not with this Outlook Object Model example: it relies on classic Outlook being installed and configured. For a process that must run independently or unattended, use an organization-approved service or API designed for that purpose.

How do I send the email automatically?
After testing the message in draft form, replace .Display with .Send. Confirm the recipients, attachment, account, and organizational rules first; automation can submit a message without giving you a last visual check.

Can I use this code in the new Outlook for Windows?
No, this code uses classic Outlook’s COM automation interface, which new Outlook does not provide. Use classic Outlook or choose another supported mail workflow.

Can Excel VBA attach the workbook itself?
Yes. Use ThisWorkbook.FullName as the path after saving the workbook. If it has not been saved, first save it to a location so Outlook has a real file to attach.

Will this macro work on a Mac or in Excel for the web?
No. This sample is Windows VBA that automates Outlook through COM. Excel for the web does not run VBA, and Mac automation requires a different approach.

Why does Outlook ask for permission or refuse to send?
Security prompts and restrictions depend on the Outlook version, system configuration, and organization policy. Do not try to bypass them; ask your administrator which automation method is permitted.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.