VBA macros · Email and other Office apps

VBA macro to send the active workbook by Outlook email

Attach the active workbook, including unsaved changes, to a new Outlook email with recipient, subject and text filled in. Display first or send directly.

Windows only

Sending the current workbook usually means saving it, switching to Outlook, starting a mail, attaching the file, typing the address and the same subject line as last week. This macro does it in one go: it saves a copy of the active workbook to your temporary folder, creates an Outlook email with the copy attached and shows it so you can check it before clicking Send.

VBA
Option Explicit

Sub EmailActiveWorkbook()
    Dim wb As Workbook
    Dim olApp As Object, olMail As Object
    Dim tempFile As String

    Set wb = ActiveWorkbook
    If Len(wb.Path) = 0 Then
        MsgBox "Save the workbook once first, so it has a file name and type.", vbExclamation
        Exit Sub
    End If

    ' A copy includes unsaved changes and leaves the original untouched
    tempFile = Environ$("TEMP") & "\" & wb.Name
    wb.SaveCopyAs tempFile

    ' Use Outlook if it is running, otherwise start it
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    On Error GoTo Fail
    If olApp Is Nothing Then Set olApp = CreateObject("Outlook.Application")

    Set olMail = olApp.CreateItem(0)            ' 0 = olMailItem
    With olMail
        .To = "[email protected]"
        .CC = ""
        .Subject = "Report: " & wb.Name
        .Body = "Hello," & vbCrLf & vbCrLf & _
                "Please find the workbook attached." & vbCrLf & vbCrLf & _
                "Best regards"
        .Attachments.Add tempFile
        .Display                                ' .Send would send at once
    End With

    Kill tempFile                               ' Outlook has its own copy now
    Exit Sub

Fail:
    MsgBox "Could not create the email: " & Err.Description, vbExclamation
End Sub
Attach the active workbook to a new Outlook email

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick EmailActiveWorkbook and click Run.
  5. Replace [email protected], the subject and the text with your own before the first run.
  6. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

The workbook itself might be open with unsaved changes, so attaching the file on disk could send an old version. SaveCopyAs writes the current state to the Windows temporary folder (Environ$("TEMP")) under the same name, and that copy is what gets attached. Your open file keeps its name and saved state.

GetObject(, "Outlook.Application") connects to Outlook if it is already running; if not, it raises an error that On Error Resume Next ignores, and CreateObject starts Outlook instead. Both use late binding (variables declared As Object), so you do not need to set a reference to the Outlook library and the macro works with any Outlook version. Controlling other Office programs is explained in Control Outlook, Word and PowerPoint from Excel VBA.

CreateItem(0) makes a new email. The With block fills in the fields and attaches the copy. .Display opens the message window so you can read it and click Send yourself. After Attachments.Add, Outlook stores its own copy of the file, so the temporary copy can be deleted with Kill.

Change it

Common changes:

  • Several recipients: separate addresses with semicolons, .To = "[email protected]; [email protected]".
  • Take the recipient from a cell: .To = ThisWorkbook.Worksheets("Settings").Range("B2").Value.
  • Send without looking: replace .Display with .Send. Outlook may show a security prompt the first times; your IT department controls that.
  • Attach a PDF instead of the workbook: create the PDF with the code from VBA macro to export a range as PDF in Excel and attach that file.
  • Keep your Outlook signature: call .Display before setting the text, then set .HTMLBody = myHtml & .HTMLBody, where myHtml is your text written as HTML. That puts your text above the signature Outlook added, the same trick VBA macro to send a range in the body of an Outlook email uses.

To send one email per row of a list (for example one file per branch manager), loop through the rows and create a mail inside the loop, attaching the file for that row. Always test such loops with .Display before you switch to .Send.

What can go wrong

Watch out. These macros drive classic Outlook for Windows through its automation interface. The new Outlook for Windows, Outlook on the web and Outlook for Mac do not support it, so CreateObject fails with error 429 (ActiveX component can't create object). If you have switched to the new Outlook, turn the New Outlook toggle off, or save the file and attach it by hand.

This macro is Windows-only: Environ$("TEMP"), the backslash and Outlook automation are not available in Excel for Mac. If the workbook's name contains characters such as #, some mail servers rename the attachment; that is harmless. Very large workbooks can exceed your mail server's attachment limit (often 20 to 25 MB).

Tip. Use .Send only when you are sure of the recipients. An email sent by mistake cannot be called back reliably.

Related: VBA macro to send a range in the body of an Outlook email, VBA macro to save each sheet as a separate PDF in Excel, Control Outlook, Word and PowerPoint from Excel VBA.

More vba macros