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 –
|The window is hidden, and focus is passed to the hidden window.
|The window has focus and appears in its most recent size and position.
|The window is minimized but has focus. This is default window style.
|The window is maximized and focused.
|The window appears in its most recent size and position, and the currently active program will be focused
|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