Introduction | Example | Tutorial | Applications | Comments

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

Write better macros in half the time
I see people struggling with Excel every day and I want to help. That's why I'm giving away my personal macro library for free. This powerful gift lets you automatically import all my macros directly into your spreadsheets with just one click.

I want to join the free wellsrPRO VBA Training program

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

Comments

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.


About The VBA Tutorials Blog

Ryan Wells

The VBA Tutorials Blog was created by Ryan Wells, a Nuclear Engineer and professional VBA Developer. He created this site to help others learn to write macros in Excel. That's why he developed a unique 3-part free Excel training program to help others quickly learn VBA in a natural setting: right inside Excel.