How to Run a Windows Application with Excel VBA
In some situations, you may want to open a second Windows application while still operating in another application such as Excel. VBA, short for Visual Basic for Applications, provides a method using a few lines of code to call upon another application such as Microsoft Word while working with Excel. Although you could do the same thing by simply using the “Start” command to open the second application, using VBA makes the second program run asynchronously. That is to say, control returns instantly to Excel, which continues performing calculations without changing to the other application manually.
Instructions
-
-
1
Open a new workbook in Microsoft Excel. Click the “Developer” tab in the top-level menu. If you do not see this option, click the “Office” button in the top left corner of Excel. Select “Excel Options” from the window and click the box next to “Show Developer tab in the Ribbon.” Click “OK.”
-
2
Click the “Visual Basic” tab now visible in the row of Developer options. This opens a Visual Basic for Applications (VBA) development window.
-
-
3
Click “View” in the top menu list on the development window. Click “Code” from the drop-down list.
Enter the following code:
Private Sub CommandButton1_Click()
Dim AppName As String
'The next line sets up a way for you to choose the Windows app
'AppName = InputBox("Enter the path and executable name of the application")
'The next line shows how you have to enter the path and executable file name for the App
'AppName = "C:\Program Files(x86)\Microsoft Office\Office12\winword.exe"
'If the App is in the normal path as Word, Notepad, Excel IE, and other Microsoft standards 'are, then you do not need the full path. Enter just the actual program executable as you see.
AppName = "winword.exe"
Shell AppName, vbNormalFocus
End SubNote: The lines beginning with apostrophes do not execute.
-
4
Return to the worksheet screen, partially hidden behind the coding windows. Click the downward-pointing icon in the “Insert” tab of the Developer menu. Double-click the “Command Button” icon in the “ActiveX Controls” section. This adds the button to the Excel spreadsheet with the default name of “CommandButton1.” Drag this button with the mouse to an out-of-the-way location on your worksheet. Double-click on the button to verify that the code you entered in Step 3 shows up. If it does not, check the code screen, particularly to ensure the name of the Sub (subroutine) above matches the name on the command button.
-
5
Save the spreadsheet as a macro-enabled version. Close the spreadsheet file. Reopen it, and when warned about running macros, select the “Enable Macros” option.
-
6
Click “Home” on the top-level menu. Click the button you created on the worksheet and Microsoft Word will open. Return to the VBA code section and alter the code lines to match which applications you want to run. Remove the apostrophe in the line beginning “’AppName=Input Box” to make it executable. You will then be prompted for an application. Delete the lines beginning ‘AppName=”winword.exe” and ‘AppName=”C:\Program Files” so you can run any app you wish.
-
1
Tips & Warnings
Dress up your program by renaming the CommandButton to something more meaningful such as “Run another application.” Right-click on the button and select “Properties” to change the back-color, font and other appearances.
Because of the asynchronous nature of the Shell command, depending on the action taken under the command, the process may not finish before the next statement executes. According to “Mastering VBA,” "this can cause errors in your procedures if subsequent commands depend on the Shell statement having executed.”
References
- Mastering VBA; Guy Hart-Davis; 2005
- Naval Postgraduation School: Thesis
Resources
- Photo Credit Stockbyte/Stockbyte/Getty Images