VBA & Macro Problems

How to Automatically Email an Excel Range When a Workbook Opens

Khadija KhanKhadija Khan Nov 3, 2026 869 views

Question details

The user needs an Excel macro to automatically run when a workbook is opened, embedding a selected data range from a specific worksheet directly into the body of an Outlook email.

How to Automatically Email an Excel Range When a Workbook Opens
Product
Excel
Device & OS
not provided
Scenario
Automating the distribution of spreadsheet data via Outlook email upon opening a workbook.
Observed behavior
The Workbook_Open macro successfully triggers and creates the Outlook email, but the selected Excel range fails to display in the message body.
Before you start

Ensure that Microsoft Outlook is installed and configured with an active email account, and verify that your Excel Trust Center settings allow macros to execute.

Solution 1Recommended

Use VBA to Convert the Excel Range to HTML for the Email Body

To embed spreadsheet cells into an Outlook message natively, the defined Excel range must be converted to an HTML format before being assigned to the email body.

Standard VBA copy-paste commands do not work directly for inserting ranges into an Outlook email body. Instead, you need a helper function to convert the defined Excel range to a temporary HTML file and read it back into the email's .HTMLBody property.

It is also highly recommended to explicitly add borders to your range before conversion, as standard Excel worksheet gridlines are not carried over into the HTML output by default.

1
Open the VBA Editor

Press Alt + F11 in Excel to open the Visual Basic for Applications (VBA) Editor, and locate your project in the Project Explorer.

2
Add the Workbook_Open Event

Double-click 'ThisWorkbook' in the Project Explorer and insert your Workbook_Open event code. Define your dynamic range using code like Cells(Rows.Count, "A").End(xlUp).Row to find the last populated row.

3
Insert the HTML Helper Function

Right-click your project, choose Insert > Module, and paste a RangeToHTML helper function (such as Ron de Bruin's standard function) into this standard module.

4
Assign HTML to the Email Body

In your email generation script, call the helper function and assign the output to the .HTMLBody property of your Outlook MailItem object.

5
Test with the Display Method

Use the .Display method instead of .Send while testing your macro. Once you have verified the HTML output looks correct, change it to .Send.

Use VBA to Convert the Excel Range to HTML for the Email Body
Testing Tip: Always use the .Display method while building and testing your macro to prevent sending incomplete or erroneous emails automatically.
Advanced Spreadsheet Automation

Automate Workflows with WPS Spreadsheets

WPS Spreadsheets provides excellent support for VBA macros, allowing you to automate tasks such as sending emails and formatting data seamlessly.

  1. 1. Download and Install WPS Office: Get the latest version of WPS Office from the official website and install it on your computer.
  2. 2. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheets and open the .xlsm file containing your VBA macros.
  3. 3. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click 'Visual Basic' to access the VBA Editor.
  4. 4. Run and Edit Your Macros: Edit your Workbook_Open event and helper functions, and run your automated email scripts exactly as you would in standard Excel.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) formatsRobust built-in VBA environment for macro executionLightweight application that loads workbooks quicklyFree to download and use for daily productivity tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why is the Excel range appearing as plain text or losing its formatting in the email?

If you assign the range directly to the `.Body` property of the Outlook item instead of `.HTMLBody`, all cell formatting will be lost. Ensure you use an HTML conversion function and assign the result strictly to `.HTMLBody`.

How do I dynamically select only the rows with data?

You can use `Cells(Rows.Count, "A").End(xlUp).Row` in your VBA code to find the last populated row in column A. This allows you to define a dynamic range, such as `Range("A1:C" & LastRow)`.

Why don't the Excel gridlines show up in the Outlook email?

Standard worksheet gridlines are not included during the HTML conversion process. To make them visible in the email, you must explicitly apply cell borders to the Excel range using VBA or standard formatting before triggering the email script.