logo
search
VBA & Macro Problems

How to Fix VBA Kill Errors for Files Stored in OneDrive

Tauseeq MagsiTauseeq Magsi Oct 9, 2026 869 views

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.

How to Fix VBA Kill Errors for Files Stored in OneDrive
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.
Before you start

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.

Solution 1Recommended

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.

1
Identify your local sync folder

Locate your local OneDrive synchronization folder path, which typically looks like C:\Users\[YourUsername]\OneDrive.

2
Update the VBA script path

Modify your VBA script to reference the local file path instead of the cloud URL for both the PDF generation and the Kill command.

3
Add a synchronization delay

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.

4
Execute the Kill command

Run the Kill command using the updated local path to successfully delete the old PDF file before generating the new one.

Use Local Synchronized Paths Instead of Cloud URLs
Dynamic Paths for Shared Macros: You can use the Environ("UserProfile") function in your VBA code to dynamically build the local OneDrive path, ensuring the macro works seamlessly for multiple users sharing the same file.
Free Microsoft Office alternative

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. 1. Download and install: Download WPS Office for free from the official website and run the quick installation.
  2. 2. Open your macro files: Open your existing .xlsm or .xls files directly in WPS Spreadsheet without format conversion.
  3. 3. Enable macros: Click 'Enable Macros' when prompted by the security warning to continue using your VBA scripts seamlessly.
Completely free and lightweight alternative to Microsoft Office.Fully compatible with Microsoft Excel macro-enabled formats like .xlsm and .xlsx.Robust built-in support for VBA macros and complex spreadsheet automation.Familiar user interface ensuring zero learning curve and seamless migration.
microsoft office alternative - wps office

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.