Shell Function in VBA
VBA

Shell Function in VBA with Example

Shell function in VBA is very useful. If you want to start a program or open a executable file then you can use shell function in VBA . In this article we will explain shell command for below 3 examples-

  • Start a Program
  • Open a Folder
  • Open a Non-Excel file

Below is the syntax of Shell function-

Shell (pathname, windowstyle)

Pathname: The full path of the program to execute (String).

WindowStyle: Windowstyle is an optional argument. If it is omitted, the program starts minimized with focus.

Below is the list of windowstyle Constant and numeric values –

ConstantValueDescription
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 default window style.
vbMaximizedFocus3The window is maximized and focused.
vbNormalNoFocus4The window appears in its most recent size and position, and the currently active program will be focused
vbMinimizedNoFocus6The window is minimized, the currently active program will be focused.

 

Start a Program:

In the path name you type the program name like, Notepad, Calc, WinWord etc.

VBA.Shell "Notepad", vbNormalFocus

 

Open a Folder:

To open a folder, we need to use “Explorer.Exe FolderPath” as given in below example-

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

 

Open a Non-Excel File:

To open a Non-Excel File, we need to use “Explorer.Exe FolderPath/FilenamewithExtension” as given in below example-

VBA.Shell "Explorer.exe C:\Users\Priyendra.Kumar\Desktop\MyFolder\PDF Files\Test-1.pdf", vbNormalFocus

 

 Click here to download the Practice file

Watch the step by step video tutorial:

PK
My Name is PK. I am founder of PK-AnExcelExpert.com. I am a Microsoft Certified Professional. I have more than 10 years of experience in Data Visualization, Excel Automation and dashboard creation. Excel is my passion and I like to do always something innovative in Excel and share it with other people.
https://www.pk-anexcelexpert.com