logo
search
VBA & Macro Problems

How to Open a Specific Folder in Windows File Explorer Using VBA

Khadija KhanKhadija Khan Oct 12, 2026 870 views

Question details

The user wants to write a VBA macro to open Windows File Explorer and navigate directly to a specific folder path.

How to Open a Specific Folder in Windows File Explorer Using VBA
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.
Before you start

Verify that the exact folder path exists on your Windows system and ensure your Office application has macro execution enabled.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in your spreadsheet application to open the Visual Basic for Applications (VBA) window.

2
Insert a New Module

Click 'Insert' > 'Module' from the top menu to create a blank script.

3
Declare the ShellExecute API

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`.

4
Create the Wrapper Subroutine

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)`.

5
Run the Macro

Call your subroutine by passing the target path: `ShellToFile "C:\Users\YourName\Documents\MyFolder"`.

Use the Windows API ShellExecute Function
Path Verification: Always ensure the folder path exists before executing the script to avoid silent failures.

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. 1. Install WPS Office: Download and install the free version of WPS Office from the official website.
  2. 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing macro-enabled workbook.
  3. 3. Access the VBA Editor: Navigate to the 'Tools' tab and click on 'Macro' to open the built-in VBA editor.
  4. 4. Execute Your Script: Paste your folder-opening script into a module and run it seamlessly.
Fully compatible with Microsoft Office VBA macros and scriptsNative support for opening, editing, and saving .xlsm and .xls filesFree and lightweight spreadsheet solution with a familiar UIAdvanced developer tools built directly into the software
microsoft office alternative - wps office

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.