VBA & Macro Problems

Fix the Wrong Yahoo Mail Attachment in an Excel VBA Macro

John WilsonJohn Wilson Nov 20, 2026 869 views

Question details

Ensure an Excel VBA macro attaches the newly saved worksheet file when sending a message through Yahoo Mail with CDO.

Fix the Wrong Yahoo Mail Attachment in an Excel VBA Macro
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.
Before you start

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.

Solution 1Recommended

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.

1
Build the full file path

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.

2
Save the export workbook

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.

3
Check the saved path

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.

4
Attach and test the file

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.

Save and attach the worksheet using one verified full path
Use an explicit workbook reference: Keep a reference to the export workbook rather than relying on ActiveWorkbook, which can change as the macro runs. Confirm that the destination path points to the intended file, especially if a file with that name already exists.
Free Microsoft Office alternative

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.

Open and edit worksheet files in a familiar spreadsheet interfaceCompatible with Microsoft Excel formats including .xlsx and .xlsUse a free, lightweight suite for everyday Office documentsMove existing Office files into WPS without starting from scratch
microsoft office alternative - wps office

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.