Fix Excel VBA Kill Command Failing to Delete PDF Attachments
Question details
The user is encountering an issue where the VBA Kill command is unable to delete a temporary PDF file that was generated from an Excel worksheet and attached to an Outlook email.
- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Exporting an Excel worksheet to a temporary PDF, attaching that PDF to an Outlook email via a VBA macro, and subsequently attempting to delete the temporary PDF using the Kill command.
- Observed behavior
- The VBA Kill(strPath) command fails or throws an error because the PDF file is still open, locked by Outlook, or otherwise inaccessible during the script execution.
Ensure you have saved a backup of your Excel workbook and verify the exact file path where your temporary PDF is being generated before modifying your VBA macro.
Release File Lock by Delaying Deletion or Closing Outlook
The most common reason for the Kill command failing is that Outlook keeps the PDF file locked while the email message is being displayed or processed.
When a macro uses the '.Display' method for an Outlook MailItem, the email opens in the foreground, and Outlook maintains a lock on the attached PDF. As long as this lock exists, Windows will deny any request by the VBA Kill command to delete the file.
Check your VBA script where '.Display' or '.Send' is called for the Outlook MailItem. If using '.Display', understand that the file will remain locked until the email is sent or closed.
Move the 'Kill(strPath)' command to execute only after the email has been fully sent or closed. Avoid placing it immediately after the '.Display' command.
Use a delay or loop structure (such as 'On Error Resume Next' combined with 'Application.Wait') to attempt deletion multiple times until the Outlook file lock is released.
Verify File Path (strPath) and Folder Permissions
An incorrect file path or a lack of sufficient Windows folder permissions can prevent VBA from locating or deleting the temporary PDF.
Use WPS Office for Seamless PDF Exporting and Sharing
If you frequently encounter VBA script errors or file locking issues in Microsoft Office, consider switching to WPS Office. It provides a lightweight, highly compatible environment with built-in PDF tools that simplify exporting, saving, and emailing documents without needing complex macros.
- 1. Download and install: Download WPS Office for free and install it on your device.
- 2. Open your spreadsheet: Launch WPS Spreadsheets and open your existing Excel workbook.
- 3. Export and share directly: Use the built-in 'Export to PDF' tool from the Menu, and click the 'Share' button to instantly attach it to an email without writing a single line of VBA code.

Frequently Asked Questions
Why does VBA give a 'Permission Denied' error on the Kill command?
This error usually occurs because another application, such as Outlook or an active PDF viewer, currently has the file open or locked in the background. The file must be completely released by that application before VBA can delete it.
How can I force my VBA script to wait before deleting the file?
You can use the 'Application.Wait (Now + TimeValue("0:00:05"))' command to pause the macro for 5 seconds. This provides Outlook enough time to process the attachment and release the lock on the PDF before the Kill command runs.
Can I delete a file to the Recycle Bin instead of permanently deleting it with the Kill command?
No, the standard VBA 'Kill' command bypasses the Recycle Bin and deletes the file permanently from the drive. To send a file to the Recycle Bin, you must implement the Windows API 'SHFileOperation' function in your script.




