How to Change the Default Download Folder in Excel VBA
Question details
The user needs to specify a custom destination folder for files downloaded through an Excel VBA script rather than having them automatically save to the default Windows Downloads directory.

- Product
- Excel VBA
- Device & OS
- not provided
- Scenario
- Executing an Excel VBA macro to download files from the web using Internet Explorer automation and the SendKeys function.
- Observed behavior
- The files are forcefully saved to the default Windows Downloads folder, preventing the user from programmatically controlling the exact destination path.
Ensure you have access to the VBA Editor (Developer tab enabled) and back up your current macro code before modifying your download procedures.
Replace SendKeys with an XMLHTTP Request
Using an HTTP request allows you to fetch the file in the background and use an ADODB Stream to save it directly to any specified local folder.
Relying on SendKeys to interact with browser download prompts is highly unreliable. An XMLHTTP request fetches the file data directly, allowing you to build the exact target folder path and file name before committing the save.
Press ALT + F11 in your spreadsheet to open the Visual Basic for Applications window, and navigate to the module containing your download script.
Declare an 'MSXML2.XMLHTTP' object. Use the '.Open "GET", URL, False' method followed by '.Send' to request the file directly from the server without invoking a browser interface.
Use the 'Dir' function in VBA to check if your desired target folder exists. If it does not, use the 'MkDir' command to create the directory programmatically.
Declare an 'ADODB.Stream' object, set its type to binary (Type = 1), and open it. Write the 'ResponseBody' from your HTTP request into the stream, then use the '.SaveToFile' method with your specific folder path and file name.

Use the URLDownloadToFile Windows API
A clean and powerful alternative utilizing a built-in Windows API function to directly download and route files to a specific destination folder.
Run and Manage VBA Macros Seamlessly in WPS Office
WPS Spreadsheet offers an integrated, powerful Developer environment that fully supports editing, debugging, and executing VBA macros, making it incredibly easy to fix automated download paths.
- 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
- 2. Open Your Macro File: Launch WPS Spreadsheet and open your existing .xlsm or .xlsb file.
- 3. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor' to seamlessly update your file download paths.

Frequently Asked Questions
Why is SendKeys considered unreliable for downloading files in VBA?
SendKeys simulates physical keystrokes on the active window. If another program pops up, your computer lags, or the user clicks elsewhere, the keystrokes are sent to the wrong window, causing the download to fail or save incorrectly.
How do I check if my custom download folder exists before saving in VBA?
You can use the built-in VBA `Dir` function. For example, `If Dir("C:\MyFolder", vbDirectory) = "" Then` indicates the folder does not exist. You can then use the `MkDir` command to create it before attempting to save.
Can I use VBA to download a file without opening a web browser at all?
Yes. By using either an XMLHTTP request or the Windows `URLDownloadToFile` API function in your VBA code, you can download files silently in the background without launching Internet Explorer or any other browser.




