Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: DOS from Macros.
Written by Allen Wyatt (last updated May 18, 2024)
This tip applies to Excel 97, 2000, 2002, and 2003
Macros are a wonderful way to expand the functionality of a program such as Excel. You may, however, want to expand that functionality even more by executing a DOS batch file from your macro. Excel allows you to do this by using the Shell command. The general syntax for the command is as follows:
dRetVal = Shell("myfile.bat", mode)
Within the quote marks you can place the full path name and file name of the file you want to execute. On some systems you may experience problems if you use a path name with the file specification. (This seems to crop its ugly head if you have complex path names or if the path name includes spaces.) If you have this problem, then simply use the ChDir command just prior to Shell in order to change the directory used by Excel. You can then execute Shell using just a file name.
The mode indicator simply tells Excel how you want the window opened for the file to appear. The mode indicator can be any of the following:
Value | Variable Nam | Meaning | ||
---|---|---|---|---|
0 | vbHide | Window is hidden and has focus. | ||
1 | vbNormalFocus | Window is the normal size and has focus. | ||
2 | vbMinimizedFocus | Window is minimized and has focus. | ||
3 | vbMaximizedFocus | Window is maximized and has focus. | ||
4 | vbNormalNoFocus | Window is normal size, but doesn't have focus. | ||
6 | vbMinimizedNoFocus | Window is minimized, but doesn't have focus. |
The Shell command returns a value that indicates the program ID of the file you executed, or else a zero. If a zero is returned, then there was an error executing the file.
You should remember that when you use Shell, the target file is executed right away, and it is executed independently of Excel. This means that the next macro command, in your Excel macro, is immediately executed without waiting for the Shell target file to finish. Unfortunately, there is no way around this behavior.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2506) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: DOS from Macros.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Does your macro need to know how many windows Excel has open? You can determine it by using the Count property of the ...
Discover MoreA great way to customize Excel is to add your macros to a toolbar. That way you can run them quickly and easily.
Discover MoreWant to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments