Home>Blogs>VBA>Shell Function in VBA: Open Programs, Folders and Files
Shell Function in VBA: Open Programs, Folders and Files
VBA

Shell Function in VBA: Open Programs, Folders and Files

The Shell function in VBA lets your macro start another program, exactly as if you had double-clicked it. In this tutorial you use it in the three ways PK shows in his video: start a program such as Notepad, Word or Access, open a folder in File Explorer, and open a non-Excel file such as a PDF in its own application. Each example is a single line of code, and the free practice workbook has all of them ready to run. You will also see the window style options, a few traps with file paths, and when another method is the better choice.

Shell function in VBA example code that opens Notepad, Word, MS Access, a folder and a PDF file
The Shell_In_VBA macro from the practice file: three programs, one folder and one PDF file.

⬇️  Download the Shell Function Practice File
.zip containing a macro-enabled .xlsm · 1 module with the Shell_In_VBA macro · change the paths to your own

Watch the Video Tutorial

PK opens Notepad, Word and MS Access with Shell, then opens a folder of PDF files and finally opens one of those PDFs directly from VBA.

What You Will Build

  • A Shell_In_VBA macro in a standard module.
  • Program launchers for Notepad, Word and MS Access.
  • A folder opener that shows any folder in File Explorer.
  • A file opener that opens a PDF (or any non-Excel file) in its own application.
  • Control over the window with the window style argument.

Shell Function in VBA: Syntax

Shell(pathname, windowstyle)
  • pathname (required): the program to run, as text. It can be a program name such as Notepad or a full path, followed by any arguments, such as a folder or file path.
  • windowstyle (optional): how the program’s window appears. If you leave it out, the program starts minimized with focus, which is rarely what you want, so it is good practice to always pass it.

You can write VBA.Shell or just Shell; both call the same function. The VBA. prefix simply makes it clear where the function comes from.

Window style constants

ConstantValueWhat happens
vbHide0The window is hidden, and focus is passed to the hidden window.
vbNormalFocus1The window has focus and appears in its most recent size and position.
vbMinimizedFocus2The window is minimized but has focus. This is the default.
vbMaximizedFocus3The window is maximized and has focus.
vbNormalNoFocus4The window appears in its most recent size and position; the active program keeps focus.
vbMinimizedNoFocus6The window is minimized; the active program keeps focus.

All examples in this tutorial use vbNormalFocus, so the new window comes to the front at its normal size.

Set Up the Macro

Step 1: Open the Visual Basic Editor and insert a module

Go to Developer → Visual Basic or press Alt+F11. Choose Insert → Module and start a new procedure:

Option Explicit

Sub Shell_In_VBA()

End Sub

Save the workbook as a macro-enabled file (.xlsm) so the code is kept.

Example 1: Start a Program

Step 2: Open Notepad

For the pathname, type the name of the program. Notepad lives in the Windows folder, so its name is enough:

VBA.Shell "Notepad", vbNormalFocus

Run the macro with F5 and a new, untitled Notepad window opens.

Step 3: Open Word or MS Access

Change the program name to open other applications. winword starts Microsoft Word and MSAccess starts Microsoft Access:

VBA.Shell "winword", vbNormalFocus
VBA.Shell "MSAccess", vbNormalFocus

In the video both open straight away. Comment out the lines you have tested (put an apostrophe in front) so that the next run only executes the example you are working on.

Example 2: Open a Folder

Step 4: Use Explorer.exe with the folder path

A folder is not a program, so you start Explorer.exe (Windows File Explorer) and give it the folder as an argument. Type Explorer.exe, one space, then the folder path. The easiest way to get the path is to copy it from the address bar of File Explorer:

'use your path
VBA.Shell "Explorer.exe C:\Users\Priyendra.Kumar\Desktop\MyFolder\PDF Files", vbNormalFocus

Run it and the PDF Files folder opens in a new Explorer window. In the practice file this path belongs to PK’s computer, so replace it with a folder that exists on yours.

Example 3: Open a Non-Excel File

Step 5: Add the file name and extension

To open a file, extend the same pattern: Explorer.exe, one space, the folder path, a backslash, then the file name with its extension. Explorer opens the file in the program Windows associates with that extension, so a .pdf opens in your PDF reader:

'use your path and file name
VBA.Shell "Explorer.exe C:\Users\Priyendra.Kumar\Desktop\MyFolder\PDF Files\Test-1.pdf", vbNormalFocus

In the video, Test-1.pdf opens in Adobe Reader, in PDF view and not inside Excel. The same works for Word documents, Access databases, images or text files.

Why not use Shell for Excel files?

You could open an Excel workbook this way too, but PK recommends against it. For Excel files, Workbooks.Open is the right tool: it opens the file in the current Excel session and gives your code a Workbook object to work with. Shell is for everything that is not an Excel workbook.

The Complete Macro

This is the exact code in the practice workbook. All five lines are active, so running it as it is opens Notepad, Word, Access, the folder and the PDF one after another. Comment out what you do not need, and change both paths first.

Option Explicit
Sub Shell_In_VBA()
'******* Start a Program ******
VBA.Shell "Notepad", vbNormalFocus
VBA.Shell "winword", vbNormalFocus
VBA.Shell "MSAccess", vbNormalFocus
'********** Open a Folder ***************
'use your path
VBA.Shell "Explorer.exe C:\Users\Priyendra.Kumar\Desktop\MyFolder\PDF Files", vbNormalFocus
'******** Open a Non Excel File ********
'use your path and file name
VBA.Shell "Explorer.exe C:\Users\Priyendra.Kumar\Desktop\MyFolder\PDF Files\Test-1.pdf", vbNormalFocus
End Sub

How the Shell Function Works

This section and the ones below it (errors, alternatives, tips) are additions from general VBA knowledge; they are not covered in the video.

Shell asks Windows to start a new process and hands it the text you pass. Three things are worth knowing:

  • It returns a task ID. If the program starts, Shell returns a number that identifies it, for example id = Shell("Notepad", vbNormalFocus). You rarely need it, but it confirms the start.
  • It does not wait. Your macro carries on immediately while the other program is still loading. Do not write code that expects the program to be ready on the next line.
  • It needs something it can find. Short names such as Notepad or Explorer.exe work because Windows knows where they live. For anything else, give the full path to the .exe.

Build the path from the workbook’s folder

Hard-coded paths break as soon as the file moves to another computer. Build the path from ThisWorkbook.Path instead, so a folder that sits next to the workbook is always found:

Sub Open_Folder_Next_To_Workbook()
    Dim f As String
    f = ThisWorkbook.Path & "\PDF Files"
    VBA.Shell "Explorer.exe " & Chr(34) & f & Chr(34), vbNormalFocus
End Sub

Chr(34) is a double quote. Wrapping the path in quotes keeps paths with spaces in one piece, which is the safest habit for any argument you pass to a program.

Common Errors and Fixes

Run-time error 53: File not found

Windows cannot find the program you named. Check the spelling, or give the full path of the .exe file, for example the full path to your PDF reader or to an Office program.

Explorer opens the Documents folder instead of yours

The folder path is wrong or does not exist, so Explorer falls back to a default location. Copy the path again from the address bar, and check it with Dir(path, vbDirectory) before calling Shell.

The program opens minimized or behind Excel

You left out the window style, so the default vbMinimizedFocus was used. Pass vbNormalFocus or vbMaximizedFocus.

Alternatives to Shell

  • Workbooks.Open for Excel files, as PK recommends.
  • ThisWorkbook.FollowHyperlink opens a file, folder or web address with its default program, without Explorer.exe: ThisWorkbook.FollowHyperlink ThisWorkbook.Path & "\PDF Files\Test-1.pdf".
  • In a worksheet, the HYPERLINK function gives users a clickable link to the same file without any code.

Tips

  • Test one Shell line at a time and comment out the rest, just like in the video.
  • Keep the files your macro opens in a subfolder of the workbook’s folder and use ThisWorkbook.Path.
  • Never pass text typed by a user straight to Shell; it can run any command. Validate it or use FollowHyperlink.
  • Microsoft lists every argument in the Shell function reference.

Want It Ready-Made?

If you would like finished automation tools instead of writing each macro yourself, the Excel VBA Tools & Utilities Pack on NextGenTemplates.com bundles ready-to-use VBA templates. You can also browse the full Excel VBA tools collection.

Related Tutorials

Frequently Asked Questions

What does the Shell function do in VBA?

It starts another program from your macro. You pass the program name or path, plus optional arguments such as a folder or file, and a window style that controls how the window appears.

How do I open a folder with VBA?

Call Shell with Explorer.exe, one space and the folder path, and use vbNormalFocus as the window style. Example 2 shows the exact line.

How do I open a PDF file from Excel VBA?

Use the same Explorer.exe pattern with the full file path, including the .pdf extension. Windows opens it in your default PDF reader.

What is the default window style of Shell?

If you leave the argument out, the program starts minimized with focus (vbMinimizedFocus, value 2). Pass vbNormalFocus to see the window at its normal size.

Should I use Shell to open Excel workbooks?

No. Use Workbooks.Open for Excel files, because it opens them in the current Excel session and lets your code work with the workbook. Shell is meant for other programs and files.

About the Author

PK is a Microsoft Excel expert and trainer and the founder of PK: An Excel Expert and NextGenTemplates.com. He has been teaching Excel, VBA, Power Query and Power BI since 2016 on his YouTube channel.

Conclusion

The Shell function in VBA is a one-line way to reach outside Excel: give it a program name to start Notepad, Word or Access, give Explorer.exe a folder path to open a folder, or add a file name to open a PDF in its own application. Always pass a window style, use full or workbook-relative paths, and keep Workbooks.Open for Excel files. Download the practice file, change the paths and try each example.

PK
Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!
https://www.pk-anexcelexpert.com