To open an external application using VBA in Excel, use the Shell function for simple launches or the CreateObject method for deeper integration and control. The Shell function is the fastest way to trigger an .exe file, while CreateObject allows you to manipulate the application after it opens. This capability is essential for automating workflows that span multiple software programs, such as launching a PDF reader to show a report or opening Notepad to log errors.
What You'll Need
- Microsoft Excel installed on a Windows PC.
- The Developer tab enabled in the Excel Ribbon.
- The full file path to the executable (.exe) of the application you wish to open.
- Basic familiarity with the VBA Editor (VBE).
Step-by-Step
- Enable the Developer Tab: If you do not see the Developer tab at the top of your Excel screen, right-click any existing tab on the Ribbon and select Customize the Ribbon. In the right-hand list, check the box next to Developer and click OK.
- Open the VBA Editor: Click on the Developer tab and select Visual Basic, or press
Alt + F11on your keyboard. This opens the integrated development environment where your code will live. - Insert a New Module: In the Project Explorer on the left, right-click on your workbook name, hover over Insert, and select Module. This creates a clean sheet of code separate from specific worksheets.
- Write the Shell Command: To open a basic application like Notepad, type the following code into the module:
Sub OpenNotepad() Shell "notepad.exe", vbNormalFocus End SubFor applications that are not in the Windows system path, you must provide the full directory path. For example, to open a specific browser or third-party tool:Sub OpenCustomApp() Dim appPath As String appPath = "C:\Program Files\CustomApp\app.exe" Shell appPath, vbNormalFocus End SubNote that in VBA, backslashes in file paths must be enclosed in quotes, and the path must be exact. - Using CreateObject for Advanced Control: If you need to not only open the app but also send data to it (like opening Word and typing a sentence), use the
CreateObjectmethod. Example for Microsoft Word:Sub OpenWordAndWrite() Dim wordApp As Object Set wordApp = CreateObject("Word.Application") wordApp.Visible = True wordApp.Documents.Add wordApp.Selection.TypeText "Hello from Excel VBA!" End Sub - Run the Macro: You can run the code by pressing
F5while the cursor is inside the subroutine, or return to Excel, click the Macros button on the Developer tab, select your macro name, and click Run. - Assign to a Button (Optional): To make the process user-friendly, go to the Developer tab, click Insert, and choose the Button (Form Control). Draw the button on your sheet, and when the assignment window appears, select your macro and click OK.
Common Mistakes
One of the most frequent errors is the File not found error. This occurs when the path provided to the Shell function is incorrect or contains a typo. Because VBA does not automatically browse for the file, a single missing backslash or a misspelled folder name will cause the code to fail. Always verify the path by copying it directly from the Windows File Explorer address bar.
Another common mistake is forgetting to set the Visible property when using CreateObject. When you use CreateObject("Word.Application") or CreateObject("Excel.Application"), the application launches in the background (headless mode). If you do not explicitly write wordApp.Visible = True, the user will think the macro didn't work because nothing appeared on the screen, even though the process is running in the Task Manager.
Using the wrong window style constant is also common. Using vbHide instead of vbNormalFocus will launch the application in a hidden state, which is rarely the intended result for a user-facing tool. Ensure you use vbNormalFocus or vbMaximizedFocus to ensure the application takes the user's attention.
Tips
To make your code more flexible, avoid hard-coding the application path. Instead, store the path in a hidden cell in your Excel workbook. Use appPath = Range("Z1").Value. This allows you to update the application path if the software is moved to a different folder or updated to a new version without having to edit the VBA code itself.
When dealing with paths that contain spaces (e.g., C:\Program Files\My App\app.exe), the Shell function can sometimes struggle. To fix this, wrap the path in double quotes within the string. In VBA, this is done by using triple quotes: Shell """C:\Program Files\My App\app.exe""", vbNormalFocus. This ensures Windows interprets the entire string as a single path rather than stopping at the first space.
If you are opening a specific file rather than just an application, consider using the ShellExecute API or the FollowHyperlink method. ActiveWorkbook.FollowHyperlink "C:\Documents\Report.pdf" is often easier than finding the path to the PDF reader executable because Windows will automatically use the default associated application for that file type.
Troubleshooting
If the application fails to open, start by checking the Task Manager (Ctrl + Shift + Esc). Look for the application in the "Background processes" section. If it is there but not visible, you likely missed the .Visible = True command in a CreateObject script. End the task and correct the code.
If you receive a "Permission Denied" error, it is likely that the application requires administrative privileges to run. VBA runs with the permissions of the Excel instance. Try running Excel as an Administrator (right-click the Excel icon > Run as administrator) and then execute the macro. If this works, you may need to adjust the permissions of the target .exe file in Windows security settings.
If the code runs without error but nothing happens, double-check the executable name. Some applications have a launcher (e.g., launcher.exe) and the actual program (e.g., main.exe). Ensure you are calling the correct entry point. Additionally, verify that the application is not already open in a state that prevents a second instance from launching.
Frequently Asked Questions
Can I open a website instead of an app?
Yes, the simplest way is to use ActiveWorkbook.FollowHyperlink "https://www.google.com". This opens the URL in the user's default web browser without needing the path to the browser's .exe file.
Will this work on Mac versions of Excel?
No, the Shell function and CreateObject for Windows apps are specific to the Windows API. Mac VBA uses AppleScriptTask or the Shell command with Unix-based paths, which requires a completely different coding approach.
Can I close the application using VBA after I'm done?
If you used CreateObject, you can use wordApp.Quit. If you used the Shell function, you cannot easily close the app because Shell does not return an object you can control; you would need to use more complex Windows API calls to find the process ID and kill it.
How do I open a specific file with the app?
Add the file path as an argument after the application path in the Shell function. For example: Shell "notepad.exe C:\test\notes.txt", vbNormalFocus. This tells Windows to launch Notepad and immediately load the specified text file.
Does this work for apps from the Microsoft Store?
Apps from the Microsoft Store often use "App Execution Aliases" rather than traditional .exe paths in Program Files. You can often launch them by using their alias name (like calc.exe for Calculator) in the Shell function, but finding the path for others may require looking in the WindowsApps hidden folder.