VBA & Macro Problems

Fix Excel VBA PDF Export and Outlook Attachment Errors

Aamir Naveed AkramAamir Naveed Akram Oct 23, 2026 868 views

Question details

An Excel VBA macro fails with a 'file not found' error when attempting to attach a newly exported PDF document to an Outlook email.

How to Fix Excel VBA PDF Export and Outlook Attachment Errors
Product
Excel
Device & OS
not provided
Scenario
A user is running an Excel macro designed to export a specific cell range as a PDF file and automatically attach that resulting file to a new Outlook email draft.
Observed behavior
The macro successfully runs initially but stops and reports that the PDF cannot be found during the Outlook attachment phase, usually because the exported filename and the attachment filename do not match.
Before you start

Press Alt + F11 in Excel to open the Visual Basic for Applications (VBA) editor, and locate the module containing your PDF export and Outlook email macro.

Solution 1Recommended

Use a Single Explicit Variable for the File Path

By declaring one unified string variable for the PDF's full file path, you ensure both the export method and the attachment method are referencing the exact same file.

This error almost always occurs when the path or filename in the ExportAsFixedFormat method differs slightly from the path used in the Attachments.Add method. Hardcoding paths in multiple places increases the risk of a typo or mismatched filenames (such as exporting a file named 'Feedback.pdf' but telling Outlook to attach 'Coaching.pdf'). Using a single explicit variable eliminates this inconsistency.

1
Declare a path variable

At the beginning of your macro, declare a string variable to hold the complete file path. For example, type 'Dim pdfPath As String'.

2
Assign the exact file path

Assign the full destination path and filename to your variable, such as 'pdfPath = "C:\Users\YourName\Documents\Report.pdf"'.

3
Update the PDF export command

Modify your ExportAsFixedFormat line to use the new variable. It should look similar to: 'ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath'.

4
Update the Outlook attachment command

Locate the section of code generating the Outlook email and update the attachment line to reference the same variable: '.Attachments.Add pdfPath'.

5
Run and test the macro

Press F5 or run the macro from your workbook to verify that the PDF is correctly generated and immediately attached to the Outlook draft without errors.

Use a Single Explicit Variable for the File Path
Tip for Dynamic Filenames: If you want the filename to change dynamically based on a cell value or date, you can construct the string like this: pdfPath = "C:\Path\" & Range("A1").Value & Format(Date, "yyyy-mm-dd") & ".pdf".
Free Microsoft Office alternative

Experience Seamless VBA Compatibility with WPS Office

If you frequently encounter complex macro issues or need a more reliable environment for your spreadsheets, WPS Office provides a free, lightweight, and highly compatible alternative to Microsoft Office. It offers robust support for advanced Excel features, including VBA macros (in supported versions), seamless PDF exporting, and effortless document sharing.

  1. 1. Download and Install WPS Office: Visit the official WPS website to download the free suite and install it on your device in minutes.
  2. 2. Open your Excel files: Open your existing .xlsx or .xlsm files directly in WPS Spreadsheet. The formatting and data will remain perfectly intact.
  3. 3. Export to PDF instantly: Use the built-in 'Export to PDF' button on the main toolbar to instantly convert your spreadsheets into high-quality PDFs without writing a single line of VBA code.
Excellent compatibility with Microsoft Excel formats (.xlsx, .xls, .xlsm)Built-in, one-click Export to PDF without needing complex macrosFamiliar user interface requiring zero learning curveLightweight design that runs smoothly on Windows, Mac, and mobile
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro attach an older version of the PDF?

This usually happens because the macro fails to overwrite the existing file during the export process, often due to the file being open in another program. Ensure the PDF is closed before running the macro, or add a line of code to delete the existing file before exporting.

Can I export just a specific cell range to a PDF using VBA?

Yes. Instead of applying the ExportAsFixedFormat method to the ActiveSheet or ActiveWorkbook, you can apply it directly to a specified range, such as Worksheets("Sheet1").Range("A1:D20").ExportAsFixedFormat.

What causes a 'File Not Found' error when attaching a document in Outlook VBA?

A 'File Not Found' error occurs if the file path is misspelled, if the directory does not exist, or if the code attempts to attach the document before the operating system has finished saving it to the hard drive. Using a unified path variable and sometimes adding a brief pause (DoEvents) can resolve this.