Fix the Wrong Yahoo Mail Attachment in an Excel VBA Macro
Question details
Ensure an Excel VBA macro attaches the newly saved worksheet file when sending a message through Yahoo Mail with CDO.

- Product
- Microsoft Excel VBA and Yahoo Mail
- Device & OS
- not provided
- Scenario
- A macro saves the active worksheet as a file and emails it as an attachment using CDO.
- Observed behavior
- The message may contain the wrong attachment when the path is incomplete, the save has not completed, or the macro uses the original workbook name.
Open the workbook and press Alt+F11 to locate the VBA procedure that saves the worksheet and creates the CDO message. Identify the destination folder and the workbook variable used for the worksheet export.
Save and attach the worksheet using one verified full path
Use the same absolute path for saving and attaching the exported workbook so the macro does not pick up a different file.
The example assumes exportBook is a workbook object already containing the worksheet to send, and msg is an initialized CDO message. Use a destination folder that exists and a file name that matches the selected file format.
In the VBA editor, define a String variable such as savedFilePath and set it to the full destination, for example: savedFilePath = "C:\Exports\WorksheetExport.xlsx". Replace the folder and name with your intended destination.
Use the workbook object containing the exported worksheet and save it with the same variable: exportBook.SaveAs Filename:=savedFilePath, FileFormat:=xlOpenXMLWorkbook. This example creates an .xlsx file, which does not retain VBA macros.
After SaveAs returns, add Debug.Print savedFilePath and open View > Immediate Window in the VBA editor to inspect the path. Add a file check before attaching: If Dir(savedFilePath) = "" Then Err.Raise vbObjectError + 1000, , "Export file not found: " & savedFilePath.
In the CDO message code, replace any original workbook name or relative attachment path with msg.AddAttachment savedFilePath. Run the macro and check the received attachment's name and contents before using it routinely.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and support for common Microsoft Office formats, making it easy to move everyday spreadsheet work. It does not fix the Excel VBA or CDO attachment code.

Frequently Asked Questions
Why does my macro attach the original workbook instead of the exported worksheet?
The attachment statement may use the original workbook's name or path. Store the export path in savedFilePath, use it in exportBook.SaveAs, and then use msg.AddAttachment savedFilePath.
How can I see which path the macro is attaching?
Add Debug.Print savedFilePath before msg.AddAttachment, then open View > Immediate Window in the VBA editor and run the macro. Check that the printed folder and file name match the intended export.
Does checking Dir prove the attachment contains the right worksheet?
No. Dir(savedFilePath) confirms that a file exists at that path, but it does not verify its contents. Open the saved file or inspect the received attachment to confirm it contains the intended worksheet.
Why should I avoid ActiveWorkbook in the attachment code?
ActiveWorkbook can change when the macro creates or activates another workbook. Keep an explicit reference to the export workbook and use the saved full path for the attachment instead.




