Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Send a Range as Email Body

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To send an Excel range as the body of an Outlook email, use VBA to copy the range, paste it into a new mail item, and then display or send that item. The shortest working method is: select your range, run a macro that creates an Outlook mail item, uses the Excel range's Copy method, pastes into the mail item's HTMLBody or uses WordEditor to preserve formatting, then calls .Display or .Send. This keeps the table formatting and avoids manually recreating it in the email. You need Excel and Outlook on the same machine (desktop versions), and Outlook must be configured with an email account. The steps below use late binding so you don't need to set a reference, but early binding is also shown as an option.

What You'll Need

  • Microsoft Excel (desktop version, Windows or Mac). The VBA editor is not available in Excel for the web.
  • Microsoft Outlook (desktop version) configured with at least one email account. The macros below use Outlook's object model; other email clients are not supported by this method.
  • Macro security settings that allow macros to run. You may need to enable macros for the workbook or trust the location.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, or Developer tab > Visual Basic).

Step-by-Step

  1. Prepare your data. Format the range you want to send as a clean table: avoid merged cells, blank rows, and overly wide columns. Select the exact range (for example, Sheet1.Range("A1:D10")) or create a named range. If you plan to send regularly, a named range makes the macro reusable.
  2. Open the VBA editor. Press Alt+F11 (Windows) or go to Developer > Visual Basic (if the Developer tab is not visible, enable it in Excel Options > Customize Ribbon). In the Project Explorer, right-click your workbook and choose Insert > Module.
  3. Paste the macro. Use the code below as a starting point. It uses late binding, so no reference to Outlook is required. Replace the placeholder range, subject, and recipient.
    Sub SendRangeAsEmailBody()
        Dim outlookApp As Object
        Dim outlookMail As Object
        Dim rng As Range
        Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:D10")
        Set outlookApp = CreateObject("Outlook.Application")
        Set outlookMail = outlookApp.CreateItem(0)
        With outlookMail
            .To = "recipient@example.com"
            .Subject = "Data from Excel"
            .Display ' or .Send
            rng.Copy
            .GetInspector.WordEditor.Content.Paste
        End With
        Set outlookMail = Nothing
        Set outlookApp = Nothing
    End Sub
    
    This copies the range and pastes it into the email body while preserving table formatting. If you prefer to build an HTML table string instead, you can set .HTMLBody directly (see Tips).
  4. Adjust the code. Change "Sheet1" and "A1:D10" to match your data. Set the .To address and .Subject. Use .Display to review before sending, or .Send to send immediately. If you use .Send, make sure the email is correct—there is no confirmation prompt.
  5. Run the macro. Close the VBA editor, then press Alt+F8, select SendRangeAsEmailBody, and click Run. Alternatively, assign the macro to a button on the sheet. Outlook may prompt you to allow access; approve it if you trust the source.
  6. Verify the email. If you used .Display, check that the table appears correctly, the recipient is right, and the subject is set. Then click Send. If you used .Send, check your Sent Items folder to confirm.

Common Mistakes

  • Using .Send without reviewing. Sending directly can email the wrong range or recipient. Use .Display first, then switch to .Send once tested.
  • Forgetting to set the range correctly. If the range includes blank rows or merged cells, the email may look messy. Test with a small range first.
  • Not handling Outlook security prompts. Some systems show a warning when a program accesses Outlook. You may need to adjust macro security or use a trusted location; do not disable security features globally.
  • Assuming the macro works on Mac. VBA in Excel for Mac can automate Outlook for Mac, but the object model and WordEditor behavior may differ. Test on your platform.
  • Pasting after .Display without the inspector. The code above uses .GetInspector.WordEditor to paste; if you omit that, the paste may fail or go to the wrong place.

Tips

  • To avoid Outlook dependency, build an HTML string from the range and set .HTMLBody. This works even if Outlook is not installed, as long as you use another method to send (e.g., CDO). However, CDO requires SMTP configuration and is more complex.
  • Use early binding for IntelliSense: in the VBA editor, go to Tools > References and check "Microsoft Outlook Object Model". Then declare Dim outlookApp As Outlook.Application and Dim outlookMail As Outlook.MailItem. This requires Outlook installed on the development machine.
  • To include the range as an attachment instead of the body, use .Attachments.Add with a saved file. But for body content, the copy-paste method is simplest.
  • If you need to send to multiple recipients, separate addresses with semicolons in the .To string.
  • Always test with .Display before using .Send in production.

Troubleshooting

  • Macro does not run: Check that macros are enabled. In Excel, go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Enable macros with notification or for trusted locations only. Do not enable all macros without understanding the risk.
  • Outlook not found: Ensure Outlook is installed and configured. The macro uses CreateObject("Outlook.Application"); if Outlook is not the default mail client or is not installed, this fails. Install Outlook or use an alternative method.
  • Permission denied or security prompt: Your antivirus or Outlook security settings may block programmatic access. Check Outlook Trust Center > Programmatic Access. You may need to allow access or use a workaround like redeeming a certificate, but only if you understand the implications.
  • Paste fails or formatting lost: The WordEditor method requires Outlook to use Word as the email editor. If not, the paste may not work. Alternatively, build an HTML table string. Also, ensure the range is copied before pasting.
  • Email body is empty: Check that rng.Copy is executed after the mail item is created and displayed. The order matters: create mail item, display it, then copy and paste.
  • Macro works on Windows but not Mac: Outlook for Mac may not support WordEditor the same way. Test and adjust; consider using HTMLBody with a manually built table.

Frequently Asked Questions

Can I send a range as an email body without Outlook?

Yes, but it requires a different approach. You can use CDO (Collaboration Data Objects) to send via SMTP, but that requires server settings and credentials. Another option is to save the range as an HTML file and attach it, but that is not the body. For most users, Outlook is the simplest.

How do I keep the table formatting?

Use the copy-paste method with WordEditor as shown, or build an HTML table string with inline CSS and assign it to .HTMLBody. The latter gives more control but requires more code.

Can I send only visible cells?

Yes. Use rng.SpecialCells(xlCellTypeVisible).Copy instead of rng.Copy. This copies only visible cells, which is useful after filtering.

Why does the macro fail with "Object required"?

This usually means the range object was not set correctly. Check the sheet name and range address. Also ensure you have Set rng = ... before using it.

Can I automate sending on a schedule?

You can use Windows Task Scheduler to open the workbook and run the macro, but be aware of security prompts. Alternatively, use a script that runs outside Excel. This is advanced and not recommended for beginners.

Is there a way to avoid the Outlook security warning?

Some antivirus programs offer settings to allow programmatic access. Also, Outlook's Trust Center has a programmatic access setting. However, disabling security features is not recommended. Consider using a digitally signed macro or an alternative email method.