Shell in VBA
VBA

Shell Function in VBA

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.

Syntax

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:

ConstantValueDescription
vbHide0Window is hidden and focus is passed to the hidden window. The vbHide constant is not applicable on Macintosh platforms.
vbNormalFocus1Window has focus and is restored to its original size and position.
vbMinimizedFocus2Window is displayed as an icon with focus.
vbMaximizedFocus3Window is maximized with focus.
vbNormalNoFocus4Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus6Window is displayed as an icon. The currently active window remains active

Examples:

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

 

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