Quick Jump
Introduction | Example | Tutorial | Applications | Comments

Introduction - VBA Shell Function

Learn to open and close a file with the VBA Shell function. The Shell function returns the Process ID, PID, so you can kill the file when you’re done.

If you want to know how to activate a file or folder that was previously opened, check out my article on AppActivate and more.

Example - VBA Shell Function

Open and Close a Text File

Sub OpenClose()
'Open and Close a File
Dim vPID As Variant
'Launch file
vPID = Shell("notepad.exe ""C:\VBA\text.txt""", vbNormalFocus)

'Perform your actions here

'Kill file
Call Shell("TaskKill /F /PID " & CStr(vPID), vbHide)

End Sub


Coming Fall 2017

The new best way to learn VBA

Absolutely FREE when you join the waitlist

Tutorial - VBA Shell Function

Customize VBA Shell Input

Before I show you how to customize the VBA Shell function, I want to tell you there are multiple ways to invoke the Shell function. The following are all equivalent ways of opening the VBScript Timer. Notice the Shell application returns a Variant. Well, technically it returns a Double.

Different Ways to Launch Applications with VBA Shell

Sub DailyTimer()
'Run timer
Dim vPID As Variant
'Variation 1:
vPID = Shell("wscript.exe ""C:\VBScripts\Timer.vbs""", vbNormalFocus)
'Variation 2:
Shell "explorer.exe ""C:\VBScripts\Timer.vbs""", 1
'Variation 3:
Call Shell("Explorer.exe ""C:\VBScripts\Timer.vbs""", vbNormalFocus)
'Variation 4 - Open with Notepad for Editing:
vPID = Shell("notepad.exe ""C:\VBScripts\Timer.vbs""", vbNormalFocus)
End Sub

Shell takes the form Shell( pathname [, windowstyle ] ), where the windowstyle argument is optional. You can launch different applications by simply changing the pathname argument in the Shell function.

Launch Notepad with Excel Macro

Sub LaunchNotepad()
'Run Notepad
Call Shell("Explorer.exe ""C:\Windows\system32\notepad.exe""", vbNormalFocus)
End Sub

The windowstyle property is an integer corresponding to how you want the program to run. The windowstyle properties are defined below:

Constant Integer
vbHide 0
vbNormalFocus 1
vbMinimizedFocus 2
vbMaximizedFocus 3
vbNormalNoFocus 4
vbMinimizedNoFocus 6

You can use the Process ID, stored in vPID, to kill the file or application when you’re done with it. Instead of opening a file, all you have to do is invoke the TaskKill command with the same Shell function.

Call Shell("TaskKill /F /PID " & CStr(vPID), vbHide)

This time the window was hidden using the windowstyle property vbHide.
Note: When you open a file with explorer.exe, as was done in Variation 1 and Variation 2, vPID is associated with the temporary explorer.exe Process ID. Hence, you will not be able to kill your program with vPID.

To learn how to use the vPID to control previously opened files and folders, check out my article on AppActivate and more.

Application Ideas - VBA Shell Function

  1. Open Timesheet program
  2. Open Web Browser
  3. Open Terminal
    1. Connect to Server
    2. Send Commands
    3. Kill Terminal Connection with stored Process ID
  4. Add a Master Shortcut to Outlook to open all your applications
  5. Open an Exercise Reminder when you first step in the Office each morning


Now it’s your turn! Leave a comment and let me know how I can help you. Remember - Automate Responsibly and subscribe to my email list for more great content.


Coming Fall 2017

The new best way to learn VBA

Absolutely FREE when you join the waitlist

About Ryan Wells

Ryan Wells

Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA applications, including PDF VBA - a leading Excel Add-in for exporting Excel Objects, like charts and tables, to PDFs. Discover more of his popular Excel Add-ins, including Mouse To Macro and CF Shapes, at his dedicated Excel Add-ins page.