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.

DOS From Macros

by Allen Wyatt
(last updated September 19, 2015)

3

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.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Easily Changing Print Order

You can change the order in which pages are printed (normal or reversed) using the Print dialog box. What if you want a way ...

Discover More

Searching for Paragraph Marks and Line Breaks

Word allows you to search not for special characters that normally do not print such as paragraph marks and line breaks.

Discover More

Checking Your Data File

When you get ready to merge a document with a data source, you'll want to make sure that everything is "as expected" before ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

MORE EXCELTIPS (MENU)

Deleting a Macro

Don't need that old macro any more? Here's how to get rid of it so that it is no longer a part of your workbook.

Discover More

Reversing Names In Place

Do you want a way to reverse names within a cell, making them "last, first" instead of "first last?" Here's a handy macro ...

Discover More

Swapping Two Numbers

When programming macros, variables are used extensively. At some point you might want to exchange the values held by two ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 9 + 8?

2016-10-18 13:57:22

Raj

Hi, we have an issue in macros. we dont know macros.
kindly pls write a macro prg for below issue.
we have two columns A and B.
value1 10
value2 20
value3 30
Value4
value5 50
Result 100
so if user gives any value in value4, the result should be change.
we knows that this is basic but we dont know macros.

pls resolve this.


2015-09-21 13:41:33

Scott Renz

Actually, I see that my batch file makes a 0 length output file right away, and then puts data in it when it ends. So, this one check for its length.

'myprogram.bat
'simply has a one line command:
'dir
'This was just made for this demonstration.
'Instead, call whatever program you want.

Sub RunDOSprogram()
Dim objfso As Object, a As Object
ChDir Environ("UserProfile")
If Dir("output.txt") <> "" Then Kill ("output.txt")
TryAgain:
Tries = Tries + 1
If Tries > 99 Then 'retries for about 5 minutes
MsgBox ("At least 99 tries have been made to see if this program has run properly and it has not been found to have done so.")
Exit Sub
End If
RetVal = Shell("myprogram.bat > output.txt""", 3) 'Run DOS program. Create file "output.txt" when it completes.
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime 'Wait three seconds before checking.
If Dir("output.txt") = "" Then GoTo TryAgain
Set objfso = CreateObject("Scripting.FileSystemObject")
Set a = objfso.getfile("output.txt")
If a.Size <= 0 Then
Set objfso = Nothing
Set a = Nothing
GoTo TryAgain
Else
Set objfso = Nothing
Set a = Nothing
MsgBox ("Program has finished executing")
End If
End Sub


2015-09-21 12:48:16

Scott Renz

To wait until the Shell program finishes:

'myprogram.bat
'simply has a one line command:
'dir
'This was just made for this demonstration.
'Instead, call whatever program you want.

Sub RunDOSprogram()

ChDir Environ("UserProfile")
If Dir("output.txt") <> "" Then Kill ("output.txt")
TryAgain:
Tries = Tries + 1
If Tries > 99 Then 'retries for about 5 minutes
MsgBox ("At least 99 tries have been made to see if this program has run properly and it has not been found to have done so.")
Exit Sub
End If
RetVal = Shell("myprogram.bat > output.txt""", 3) 'Run DOS program. Create file "output.txt" when it completes.
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 3
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime 'Wait three seconds before checking.
If Dir("output.txt") = "" Then GoTo TryAgain
MsgBox ("Program has finished executing")

End Sub


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share