
Tips.Net > ExcelTips Home > Macros > DOS From Macros
Summary: Do you need to execute some DOS (command line) commands from within a macro? You can do so by using the Shell command, as described in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)
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.
Tip #2506 applies to Microsoft Excel versions: 97 2000 2002 2003 2007
PivotTables Got You Perplexed? ExcelTips: PivotTables for the Faint of Heart shows how you can start using Excel's PivotTable tool right away to spin your data into gold! You discover how easy it really is to crunch the numbers you need to crunch. Uncover the power of the PivotTable Wizard, how to edit PivotTables, how to format them, how to customize them, and much more.
Check out ExcelTips: PivotTables for the Faint of Heart today!
If you have tons of data to analyze, one of the best tools in Excel's arsenal is the PivotTable. Learn how to use this tool to analyze your data. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site