Introduction | Example | Tutorial | Applications

Introduction - VBA Shell Command

Learn to open and close a file with the VBA Shell command. The Shell command 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 Command

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

Make powerful macros with our free VBA Developer Kit

It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free VBA Developer Kit full of pre-built macros so you can master file I/O, arrays, strings and more - grab a copy below.

Sure, I'll take a free VBA Developer Kit

Tutorial - VBA Shell Command

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 command. 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

The Shell command 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 Command

  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

That’s all for this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form below.

Oh, and if you have a question, post it in our VBA Q&A community.


The best free VBA training on the web
I see people struggling with Excel every day and I want to help. That's why I'm giving away my 90-days to Master VBA eCourse and my entire personal macro library for free.

Let me join the wellsrPRO VBA Training program for free