To send an Excel report using a macro, you must write a VBA (Visual Basic for Applications) script that connects Excel to your email client, typically Microsoft Outlook. This process automates the repetitive task of saving a file, opening an email, attaching the document, and clicking send, allowing you to distribute reports with a single button click.

What You'll Need

  • Microsoft Excel installed on a Windows PC.
  • Microsoft Outlook installed and configured with an active email account on the same machine.
  • The "Developer" tab enabled in the Excel Ribbon.
  • The report data prepared in your workbook.

Step-by-Step

  1. Enable the Developer Tab: If you do not see the Developer tab at the top of your screen, right-click any existing tab (like Home or Insert) and select Customize the Ribbon. In the right-hand column, check the box next to Developer and click OK.
  2. Open the VBA Editor: Click the Developer tab and select the Visual Basic button, or press Alt + F11 on your keyboard.
  3. Insert a New Module: In the VBA Editor menu, go to Insert > Module. This creates a blank space where you can paste your automation code.
  4. Enter the Macro Code: Copy and paste the following code into the module window. Note that menu labels and object names may vary slightly depending on your version of Office.
    Sub SendExcelReport()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim wb As Workbook
        Dim fileName As String
        Dim filePath As String
    
        Set wb = ThisWorkbook
        
        ' Define the temporary file path and name
        filePath = Environ("temp") & "\"
        fileName = "Report_" & Format(Date, "yyyy-mm-dd") & ".xlsx"
        
        ' Save a copy of the workbook to the temp folder
        wb.SaveCopyAs filePath & fileName
        
        ' Initialize Outlook
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        On Error Resume Next
        With OutMail
            .To = "recipient@example.com"
            .CC = ""
            .BCC = ""
            .Subject = "Automated Excel Report - " & Date
            .Body = "Please find the attached report for today."
            .Attachments.Add filePath & fileName
            .Send ' Use .Display if you want to review the email before sending
        End With
        On Error GoTo 0
    
        ' Clean up
        Set OutMail = Nothing
        Set OutApp = Nothing
        
        MsgBox "Report sent successfully!"
    End Sub
            
  5. Customize the Details: In the code above, replace recipient@example.com with the actual email address of the recipient. You can also change the .Subject and .Body text to suit your needs.
  6. Save the Workbook: Because this file contains a macro, you cannot save it as a standard .xlsx file. Go to File > Save As and select Excel Macro-Enabled Workbook (.xlsm) from the file type dropdown.
  7. Create a Trigger Button: To make the macro easy to use, go to the Developer tab, click Insert, and select the Button (Form Control) icon. Draw the button on your worksheet. When the "Assign Macro" window appears, select SendExcelReport and click OK.
  8. Test the Macro: Click your new button. Outlook should process the email in the background and send the attachment.

Common Mistakes

  • Incorrect File Format: Saving the file as a .xlsx instead of .xlsm will permanently delete the macro code the next time you open the file.
  • Outlook Not Open: While the code can often launch Outlook, the macro may fail or hang if Outlook is not configured or if it is stuck on a password prompt.
  • Hardcoding Email Addresses: Putting a single email address in the code is fine for one person, but for multiple recipients, it is better to reference a cell in the spreadsheet (e.g., .To = Range("A1").Value).
  • Security Blockers: Many organizations block macros by default. If the button does nothing, check the yellow "Security Warning" bar at the top of the sheet and click Enable Content.

Tips

  • Use .Display instead of .Send: During the testing phase, change .Send to .Display. This opens the email window so you can verify the attachment and recipient before the email actually leaves your outbox.
  • Dynamic File Names: Use the Format(Date, "yyyy-mm-dd") function as shown in the code to ensure every report sent has a unique date stamp, preventing files from being overwritten in the temp folder.
  • Reference a Range: If you only need to send a specific sheet rather than the whole workbook, you can create a new temporary workbook, copy the specific sheet into it, and attach that new file instead.

Troubleshooting

If the macro does not work as expected, try these steps in order:

  1. Check Outlook Status: Ensure Outlook is open and signed into the correct account. If Outlook is closed, the CreateObject command may fail.
  2. Verify Macro Permissions: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure "Disable VBA macros with notification" is selected so you can manually enable them.
  3. Check for Dialog Boxes: Sometimes Outlook triggers a security warning asking if you want to allow a program to send an email on your behalf. Check your taskbar for any flashing Outlook icons.
  4. Debug the Code: In the VBA editor, click inside the code and press F8 to run the macro line-by-line. This allows you to see exactly which line causes the error.

Frequently Asked Questions

Can I send the report to multiple people?

Yes. You can list multiple emails in the .To field separated by semicolons (e.g., "email1@test.com; email2@test.com"), or link the code to a list of emails in an Excel column.

Does this work with Gmail or Yahoo?

This specific macro requires the Outlook desktop application. If you use Gmail or Yahoo via a web browser, this VBA method will not work because it cannot "talk" to a website in the same way it talks to the Outlook software.

Can I send the report as a PDF instead of Excel?

Yes, but it requires an additional line of code using the ExportAsFixedFormat method to convert the sheet to PDF before attaching it to the email.

Will this work on Excel for Mac?

No. The CreateObject("Outlook.Application") command is specific to Windows COM objects. Mac users require different scripting methods (AppleScript) to interact with the Mail app.

Is it possible to schedule this to send automatically?

Excel macros cannot run while the program is closed. To truly automate the timing, you would need to use a combination of Windows Task Scheduler to open the workbook and a Workbook_Open event to trigger the macro.