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 Function in VBA
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 –
Constant | Value | Description |
---|---|---|
vbHide | 0 | The window is hidden, and focus is passed to the hidden window. |
vbNormalFocus | 1 | The window has focus and appears in its most recent size and position. |
vbMinimizedFocus | 2 | The window is minimized but has focus. This is default window style. |
vbMaximizedFocus | 3 | The window is maximized and focused. |
vbNormalNoFocus | 4 | The window appears in its most recent size and position, and the currently active program will be focused |
vbMinimizedNoFocus | 6 | The 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:
Visit our YouTube channel to learn step-by-step video tutorials