When working with VBA macros, you may often need to launch external applications outside of Excel, such as opening Notepad to write logs or opening a browser to access a specific URL.
The VBA Shell function makes it easy to execute such external programs. This article explains the basic usage of the Shell function in two common patterns.
Basic Usage of the Shell Function
The Shell function runs a specified program. If successful, it returns the program’s Task ID; otherwise, it returns 0.
Basic Syntax: Shell(PathName As String, [WindowStyle As VbAppWinStyle = vbMinimizedFocus])
- PathName: Specify the path and file name of the program you want to execute as a string.
- WindowStyle: A constant that specifies how the program window is displayed (explained later).
1. Launching by Program Name Directly
Common programs included in the Windows environment variable “Path” (like Calculator calc.exe or Notepad notepad.exe) can be launched using just the file name.
Sample Code
' Launch Notepad in normal window size
Sub LaunchNotepad()
Dim taskID As Variant
' Execute "notepad.exe" using Shell function
' Open with focus in a normal window using the second argument vbNormalFocus
taskID = Shell("notepad.exe", vbNormalFocus)
' Check if execution was successful
If IsError(taskID) Then
MsgBox "Failed to launch Notepad."
Else
Debug.Print "Notepad launched with Task ID: " & taskID
End If
End Sub
2. Launching by Specifying the Full Path
For specific applications like Microsoft Edge or Google Chrome, where the path is not automatically recognized, you must specify the exact full path to the executable file.
Sample Code
' Launch Microsoft Edge
Sub LaunchWebApp()
Dim appPath As String
Dim taskID As Variant
' Store the full path of the executable in a string variable
appPath = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
' Check if the file exists
If Dir(appPath) = "" Then
MsgBox "The specified application was not found.", vbCritical
Exit Sub
End If
' Execute Shell function with the full path
taskID = Shell(appPath, vbNormalFocus)
If IsError(taskID) Then
MsgBox "Failed to launch the application."
End If
End Sub
Note: The installation location of applications may vary depending on the environment. It is safer to use the
Dirfunction to check for the file’s existence before execution.
About the WindowStyle Argument
The second argument of the Shell function allows you to specify the display style of the application window.
| Constant | Description |
| vbNormalFocus | Displays the window at its original size and gives it focus. (Default) |
| vbMinimizedFocus | Displays the window as an icon (minimized) and gives it focus. |
| vbMaximizedFocus | Displays the window maximized and gives it focus. |
| vbHide | Hides the window and activates another window. |
| vbNormalNoFocus | Displays the window at its original size but does not give it focus (the current window remains active). |
Summary
In this article, we explained how to launch external applications using the VBA Shell function.
- Common tools like Notepad can be launched by file name only.
- Specific applications require the full path to the executable.
- You can control the window style (maximized, minimized, etc.) using the second argument.
By using this function, you can expand the capabilities of your VBA macros by integrating them with other tools.
