Fix Excel VBA PDF Attachment Failing with Certain File Names
Question details
The user's Excel VBA macro successfully generates a PDF but fails to attach it to an email via AppleScript when specific file names are used.

- Product
- Microsoft Excel
- Device & OS
- macOS
- Scenario
- Automating PDF export and email attachment using a combination of Excel VBA and AppleScript.
- Observed behavior
- The macro throws a procedure error during the email attachment phase if the file name contains invalid paths, unescaped spaces, or reserved characters.
Ensure you have noted the exact error number generated by the VBA compiler and verify that your Mac's security settings permit Excel to execute AppleScript commands.
Sanitize File Names and Check AppleScript Quoting
Identify and remove reserved characters from your file names and ensure your AppleScript paths are properly quoted to handle spaces and special formatting.
When passing file paths from Excel VBA to AppleScript, special characters, colons, or unescaped spaces can break the script, causing the attachment step to fail even if the PDF was created successfully.
Review your VBA code where the PDF file name is generated. Implement a VBA Replace function to strip out invalid characters like slashes (/ or \), colons (:), or quotation marks (") before saving the PDF.
Ensure that your AppleScript receives the file path as a POSIX path (using slashes) rather than an older HFS path (using colons), as many modern mail scripts require POSIX formatting.
Wrap the file path string in escaped double quotes within your AppleScript syntax so that spaces in the file name or folder names do not fragment the file path.
Run the macro using a simple, alphanumeric file name (e.g., 'TestDocument1.pdf') in a simple directory structure to confirm the issue is isolated to file naming.

Seek Community Support on Stack Overflow
For complex integration errors between Excel VBA and AppleScript, seeking specialized help from coding communities can diagnose obscure formatting issues.
Easily Export and Email PDFs with WPS Office
Instead of relying on complex VBA scripts and AppleScript integrations that break easily, WPS Office provides a seamless, built-in feature to export your spreadsheets as PDFs and directly attach them to emails with just a few clicks.
- 1. Open your Spreadsheet: Launch WPS Office and open your spreadsheet workbook.
- 2. Export to PDF: Navigate to the 'Menu' or 'Tools' tab on the ribbon, and select 'Export to PDF'.
- 3. Share via Email: Once the PDF is generated, click the 'Share' button and choose 'Email' to automatically attach the newly created PDF to a draft in your default mail client.

Frequently Asked Questions
Why does AppleScript fail when the PDF file path contains spaces?
AppleScript typically interprets spaces as delimiters for different commands or arguments. If the entire file path string isn't enclosed in quotes, the script reads a fragmented path, leading to a file not found or attachment error.
What characters are considered invalid for Mac file names in VBA?
While macOS is lenient with file names, using colons (:), forward slashes (/), and quotation marks (") in VBA file paths can disrupt directory structures and cause AppleScript to throw unexpected syntax errors.
How do I convert a Mac path to a POSIX path in Excel VBA?
You can use the built-in MacScript function or AppleScriptTask in modern Excel for Mac versions to execute a command that converts standard HFS paths (which use colons) to POSIX paths (which use slashes) required by many mail scripts.
Are there simpler ways to email PDFs from spreadsheets without VBA?
Yes. Modern office suites like WPS Office have built-in sharing features that allow you to natively export a document to PDF and attach it to an email in a single click, completely bypassing the need for custom macro coding.




