Extract Embedded Excel Files to Create Outlook Attachments using VBA
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.
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.
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.
Open the Developer tab in Excel and click "Visual Basic", or press ALT + F11 on your keyboard to open the VBA Editor.
In the VBA Editor, right-click on your workbook name in the Project Explorer window, select "Insert", and then choose "Module".
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".
Within the same macro, create an Outlook application object using `CreateObject("Outlook.Application")` and generate a new mail item using `.CreateItem(0)`.
Use the `.Attachments.Add` method followed by the temporary file path variable to attach the newly extracted OLE object to your Outlook email.
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.
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. Install WPS Office: Download and install WPS Office, then open your macro-enabled spreadsheet (.xlsm).
- 2. Access the VBA Editor: Navigate to the "Tools" tab on the ribbon and click "Macro" or "Visual Basic Editor".
- 3. Paste Your Script: Paste your VBA code for extracting OLE objects and creating Outlook attachments into the WPS Module window.
- 4. Execute the Macro: Run the macro to automatically extract the embedded files to your local drive and generate your Outlook emails.

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")`.




