Shell Function in VBA can run executable program and returns a Variant (Double) representing the program’s task ID if successful otherwise, it returns zero.
Shell(pathname, [ windowstyle ])
pathname: Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive.
windowstyle: Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run.
The windowstyle named argument has these values:
|vbHide||0||Window is hidden and focus is passed to the hidden window. The vbHide constant is not applicable on Macintosh platforms.|
|vbNormalFocus||1||Window has focus and is restored to its original size and position.|
|vbMinimizedFocus||2||Window is displayed as an icon with focus.|
|vbMaximizedFocus||3||Window is maximized with focus.|
|vbNormalNoFocus||4||Window is restored to its most recent size and position. The currently active window remains active.|
|vbMinimizedNoFocus||6||Window is displayed as an icon. The currently active window remains active|
Below are the few examples of Shell function in VBA
Start a Program:
VBA.Shell "Notepad", vbNormalFocus VBA.Shell "winword", vbNormalFocus VBA.Shell "MSAccess", vbNormalFocus
Close a file
Dim my_notepad As Variant my_notepad = VBA.Shell("Notepad", vbNormalFocus) my_notepad = VBA.Shell("Taskkill /F /PID " & my_notepad, vbHide)
Open a Folder
VBA.Shell "Explorer.exe C:\Users\PK\Desktop\MyFolder\PDF Files", vbMaximizedFocus
Open a Non Excel File
VBA.Shell "Explorer.exe C:\Users\PK\Desktop\MyFolder\PDF Files\Test-2.pdf", vbNormalFocus