How to Email Excel PivotTable Data Through Outlook using a VBA Macro
Question details
The user needs an automated VBA solution to send an email via Outlook containing specific PivotTable and sheet data, using a recipient address dynamically determined by a PivotTable slicer selection.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating email reporting by extracting data from the 'Graphs' and 'Copy' sheets and sending it to an email address mapped on the 'Emails' sheet based on a slicer filter.
- Observed behavior
- A customized VBA macro is required to bridge the slicer selection, data copying, address lookup, and Outlook email dispatching processes.
Ensure you have Microsoft Outlook installed and configured on your computer, and verify that the 'Microsoft Outlook Object Library' is enabled in your Excel VBA Editor references.
Build a VBA Macro with Outlook Integration
Write an Excel VBA script to read the slicer value, look up the matching address, copy the PivotTable data, and generate an Outlook email.
This method requires writing a custom VBA script that interacts with the Outlook application. You will need to extract the current selection from your PivotTable slicer, match it to an email address on your 'Emails' sheet, and then copy 'PivotTable3' from the 'Graphs' sheet alongside data from the 'Copy' sheet into the email body or as an attachment.
Press Alt + F11 to open the VBA Editor. Go to Tools > References, find 'Microsoft Outlook XX.X Object Library' in the list, check the box, and click OK.
Create a new module and declare your variables. Use CreateObject("Outlook.Application") to initialize the Outlook client and set up a new MailItem object.
Write code to loop through ActiveWorkbook.SlicerCaches to identify the currently selected item. Then, use Application.WorksheetFunction.VLookup or the .Find method on your 'Emails' sheet to retrieve the matching recipient address.
Define the ranges for 'PivotTable3' on the 'Graphs' sheet and the target data on the 'Copy' sheet. You can either copy these ranges to a temporary workbook to attach them or use a custom function to convert the Excel ranges into HTML for the email body.
Set the .To, .Subject, and .HTMLBody properties of your MailItem object. Finally, use the .Display method to review the email draft, or .Send to dispatch it automatically.

Automate Your Reports with WPS Spreadsheets
WPS Spreadsheets provides robust VBA support, allowing you to run your existing Excel macros seamlessly. You can automate PivotTable reporting and Outlook email dispatching directly within WPS Office.
- 1. Open your Workbook in WPS Office: Download and install WPS Office. Open your macro-enabled workbook (.xlsm) in WPS Spreadsheets.
- 2. Enable Macros: Navigate to the 'Developer' tab on the top ribbon and ensure macros are enabled for your document.
- 3. Access the VBA Editor: Click on 'VBA Editor' in the Developer tab. Here, you can paste or edit your Outlook email automation script exactly as you would in Excel.
- 4. Run Your Automation: Use the Macros menu or assign a button in your worksheet to trigger your script and automatically send your PivotTable emails.

Frequently Asked Questions
Can I embed the PivotTable directly in the email body instead of attaching it as a file?
Yes, you can use VBA to copy the PivotTable range and paste it as HTML into the .HTMLBody property of the Outlook MailItem. This usually involves creating a secondary function like 'RangeToHTML' that converts the selected Excel range into formatted HTML code.
Why is my VBA macro failing with a 'User-defined type not defined' error?
This commonly happens if the 'Microsoft Outlook Object Library' is not enabled in your VBA Editor. Go to Tools > References in the VBA Editor and check the box for the Outlook Object Library. Alternatively, you can declare your variables as Object and use late binding with CreateObject("Outlook.Application").
How do I trigger this macro automatically when a slicer selection changes?
You can use the Worksheet_PivotTableUpdate event inside the worksheet module containing the PivotTable. Be cautious, as this event triggers on any PivotTable update. You may need to add conditional logic to check if the specific slicer was actually changed before firing the email macro.
Can I send the email to multiple addresses stored on the Emails sheet?
Yes. In your VBA code, you can loop through the matching rows on the 'Emails' sheet and concatenate multiple email addresses into a single string separated by semicolons (;). Once concatenated, pass this string to the .To or .CC property of your MailItem.




