How to Fix VBA Kill Errors for Files Stored in OneDrive
Question details
The user needs to resolve a 'File not found' error when using the VBA Kill command to delete files stored in a shared OneDrive or SharePoint folder before attaching a newly generated PDF to an email.

- Product
- VBA Macro
- Device & OS
- not provided
- Scenario
- Running a VBA macro to export a PDF, delete the old version from a shared OneDrive folder, and attach the new PDF to an Outlook email without OneDrive sync delays causing the wrong file to attach.
- Observed behavior
- The VBA Kill command returns a 'File not found' error because it is being passed a cloud URL instead of a local file path, and OneDrive synchronization delays occasionally cause the old PDF to be attached to the email.
Ensure your OneDrive or SharePoint client is actively running and syncing files to your local drive, as standard VBA file operations require access to local directories.
Use Local Synchronized Paths Instead of Cloud URLs
VBA file functions cannot process cloud-based URLs directly. Converting the OneDrive URL to a local synchronized path allows the Kill command to work properly.
VBA's built-in file operations, such as Kill, FileCopy, and Name, are designed exclusively for the local Windows file system. When a macro attempts to delete a file using a OneDrive or SharePoint URL (e.g., https://...), VBA fails to locate the file, resulting in a 'File not found' error.
Locate your local OneDrive synchronization folder path, which typically looks like C:\Users\[YourUsername]\OneDrive.
Modify your VBA script to reference the local file path instead of the cloud URL for both the PDF generation and the Kill command.
Use Application.Wait or implement a loop in your macro to pause execution for a few seconds. This ensures OneDrive completes syncing the file changes before the script attempts to attach the newly generated PDF to Outlook.
Run the Kill command using the updated local path to successfully delete the old PDF file before generating the new one.

Use Microsoft Graph or SharePoint API for Direct Cloud Access
If relying on local synchronization is not possible in your environment, use HTTP requests to interact directly with the cloud service APIs.
Try WPS Office for Seamless Macro and Spreadsheet Management
If you frequently encounter synchronization constraints or complex troubleshooting with Microsoft Office and OneDrive, consider switching to WPS Office. It provides a highly compatible, lightweight alternative with robust macro support.
- 1. Download and install: Download WPS Office for free from the official website and run the quick installation.
- 2. Open your macro files: Open your existing .xlsm or .xls files directly in WPS Spreadsheet without format conversion.
- 3. Enable macros: Click 'Enable Macros' when prompted by the security warning to continue using your VBA scripts seamlessly.

Frequently Asked Questions
Why does the VBA Kill command say 'File not found' when the file is clearly visible in my OneDrive online?
The VBA Kill command only understands local file system paths (such as C:\Users\...). If your macro references the file using a web URL (https://...), VBA cannot process it. You must configure the script to use the locally synced path of the file instead.
How can I make my VBA macro wait for OneDrive to finish syncing before attaching a file?
You can add a brief delay to your script using the 'Application.Wait' command. Alternatively, you can write a loop with the 'Dir' function to check if the new file exists locally and is free of background locks before proceeding to attach it to the email.
Can I use VBA to delete a file on SharePoint without syncing it to my local drive?
Yes, but you cannot use the standard 'Kill' command to do so. You will need to write a more advanced VBA script that utilizes the MSXML2.XMLHTTP library to send HTTP requests directly to the Microsoft Graph API or SharePoint REST API.




