How to Open a Specific Folder in Windows File Explorer Using VBA
Question details
The user wants to write a VBA macro to open Windows File Explorer and navigate directly to a specific folder path.

- Product
- WPS Spreadsheet
- Device & OS
- Windows
- Scenario
- Writing a VBA script to automate navigating to a desired directory without manually browsing.
- Observed behavior
- A basic Shell command sometimes incorrectly opens the default Documents folder instead of the intended target directory due to invalid path quoting or unhandled spaces.
Verify that the exact folder path exists on your Windows system and ensure your Office application has macro execution enabled.
Use the Windows API ShellExecute Function
The ShellExecute API is the most robust method for opening folders, as it relies on Windows native handlers and smoothly resolves system paths.
By declaring the Windows shell32.dll API, you can bypass the limitations of the standard VBA Shell command. This method ensures that the folder opens reliably even if the path contains spaces.
Press Alt + F11 in your spreadsheet application to open the Visual Basic for Applications (VBA) window.
Click 'Insert' > 'Module' from the top menu to create a blank script.
At the top of the module, type: `Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long`.
Write a subroutine that calls the API. For example: `Sub ShellToFile(strPath As String)` and inside it, use `ShellExecute(0, "Open", strPath, vbNullString, vbNullString, 1)`.
Call your subroutine by passing the target path: `ShellToFile "C:\Users\YourName\Documents\MyFolder"`.

Use the Native VBA Shell Command with explorer.exe
A simpler alternative using the built-in VBA Shell function to call explorer.exe directly.
Automate Your Workflows Using VBA in WPS Office
WPS Office fully supports VBA macros, allowing you to run scripts that interact with Windows File Explorer directly. It offers a lightweight, highly compatible environment for your spreadsheet automation needs.
- 1. Install WPS Office: Download and install the free version of WPS Office from the official website.
- 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing macro-enabled workbook.
- 3. Access the VBA Editor: Navigate to the 'Tools' tab and click on 'Macro' to open the built-in VBA editor.
- 4. Execute Your Script: Paste your folder-opening script into a module and run it seamlessly.

Frequently Asked Questions
Why does my VBA script open the default Documents folder instead of my target folder?
This typically occurs when Windows File Explorer cannot resolve the provided path. Ensure the folder exists, network drives are mapped correctly, and paths containing spaces are wrapped in quotation marks if you are using the basic Shell command.
Can I let the user browse for a folder instead of hardcoding the path?
Yes. You can use the `Application.FileDialog(msoFileDialogFolderPicker)` object in VBA. This opens a standard Windows folder-search dialog, allowing the user to select the directory dynamically and returning the chosen path to your script.
Will this VBA macro work on a Mac?
No, both `ShellExecute` and calling `explorer.exe` are exclusive to the Windows operating system. Mac users must utilize AppleScript commands via the `MacScript` or `RunAppleScript` functions to open Finder windows.




