logo
search
Attachment Issues

Extract Embedded Excel Files to Create Outlook Attachments using VBA

Maira MehtabMaira Mehtab Sep 27, 2026 871 views

Question details

The user wants to automate the process of extracting files that are embedded as OLE objects within Excel cells and attaching them to an Outlook email using VBA.

Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating email generation in Excel where the required attachments are stored as embedded OLE objects rather than external file paths.
Observed behavior
Standard VBA scripts can easily attach files using local file paths, but cannot directly attach embedded OLE objects without first extracting them to a temporary local folder.
Before you start

Create a backup copy of your Excel workbook before running or testing new VBA macros, as extracting multiple embedded OLE objects can consume significant memory and temporarily increase file size.

Solution 1Recommended

Extract OLE Objects to a Temporary Folder and Attach to Outlook via VBA

Since Outlook cannot directly attach an object embedded in Excel, you must write a VBA macro to copy the object, save it locally to a temporary path, and then attach that local file to the email.

Embedded OLE objects in Excel do not have standard file paths because they are stored within the workbook's binary structure. To attach them to an Outlook email via VBA, the script must first activate or copy the OLE object, save it to a temporary directory on your hard drive, attach the saved file to the Outlook mail item, and finally delete the temporary file to free up space.

1
Open the VBA Editor

Open the Developer tab in Excel and click "Visual Basic", or press ALT + F11 on your keyboard to open the VBA Editor.

2
Insert a New Module

In the VBA Editor, right-click on your workbook name in the Project Explorer window, select "Insert", and then choose "Module".

3
Write the Extraction Script

Write a VBA script that loops through the OLEObjects in your worksheet. You will need to use the object's native application (like Word or Adobe Acrobat) via OLE Automation to "Save As" a temporary file, saving it to a path such as Environ("Temp") & "\extracted_file.pdf".

4
Initialize Outlook Application

Within the same macro, create an Outlook application object using `CreateObject("Outlook.Application")` and generate a new mail item using `.CreateItem(0)`.

5
Attach the Extracted File

Use the `.Attachments.Add` method followed by the temporary file path variable to attach the newly extracted OLE object to your Outlook email.

6
Clean Up Temporary Files

Add a `Kill` command (e.g., `Kill tempFilePath`) at the end of your macro after the email is sent or displayed to automatically delete the temporary file from your hard drive.

VBA Complexity with Different File Types: Extracting specific file types (like PDFs versus Word documents) from OLE objects requires different approaches. While MS Office documents can often be extracted using standard Object library references, third-party formats like PDF may require interacting with Windows APIs or SendKeys to automate the "Save As" dialog.

Manage Spreadsheets and Automate Emails with WPS Office

WPS Office comes equipped with a built-in Macro editor (VBA) that allows you to run complex Excel macros to extract objects, process data, and automate Outlook email sending seamlessly.

  1. 1. Install WPS Office: Download and install WPS Office, then open your macro-enabled spreadsheet (.xlsm).
  2. 2. Access the VBA Editor: Navigate to the "Tools" tab on the ribbon and click "Macro" or "Visual Basic Editor".
  3. 3. Paste Your Script: Paste your VBA code for extracting OLE objects and creating Outlook attachments into the WPS Module window.
  4. 4. Execute the Macro: Run the macro to automatically extract the embedded files to your local drive and generate your Outlook emails.
Fully compatible with Microsoft Excel (.xlsx, .xlsm, .xls) file formatsBuilt-in powerful VBA editor for advanced automation and scriptingSeamless integration with Windows COM objects like Microsoft OutlookLightweight design with fast execution of complex data processing tasksFree to download and use for essential office productivity tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why can't I attach an embedded Excel object directly to an Outlook email via VBA?

Embedded objects (OLE objects) are stored natively within the Excel workbook's internal structure and do not have an independent file path on your hard drive. Because Outlook's `.Attachments.Add` method requires a valid local file path to attach a document, the object must be extracted and saved locally first.

Does extracting OLE objects using VBA increase my Excel file size?

The extraction process itself doesn't permanently increase the Excel workbook's file size. However, copying and processing multiple large embedded objects in memory can temporarily slow down performance. Always ensure your macro deletes the temporary files from your system drive after sending the email.

Can I extract embedded PDFs from Excel cells using a simple VBA command?

Not easily. While native Microsoft objects like embedded Word documents might be manipulated directly via OLE automation, PDFs often act as package objects. You typically need to use `SendKeys` to simulate opening the PDF in its default viewer (like Adobe Acrobat) and saving it, or use specialized Windows API calls.

How do I find the exact name of an embedded OLE object in Excel?

You can find the name by clicking on the embedded object in your worksheet, then looking at the Name Box located to the left of the formula bar (e.g., "Object 1"). In VBA, you can reference it dynamically using `ActiveSheet.OLEObjects("Object 1")`.