logo
search
VBA & Macro Problems

Fix Excel VBA Kill Command Failing to Delete PDF Attachments

Maira MehtabMaira Mehtab Sep 20, 2026 868 views

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.
Before you start

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.

Solution 1Recommended

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.

1
Review your email display method

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.

2
Reposition the Kill command

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.

3
Implement an error-handling loop

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.

File Lock Behavior: If you manually try to delete the file from File Explorer while the draft email is open, Windows will show a 'File in Use' error. This confirms the file lock is the root cause.
Free Microsoft Office alternative

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. 1. Download and install: Download WPS Office for free and install it on your device.
  2. 2. Open your spreadsheet: Launch WPS Spreadsheets and open your existing Excel workbook.
  3. 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.
Fully compatible with Microsoft Excel (.xlsx, .xls) and macro formats.Built-in robust PDF converter with direct one-click email attachment capabilities.Lightweight architecture that minimizes background process conflicts and file locks.Free to download with a familiar user interface for a seamless migration.
microsoft office alternative - wps office

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.