XL: Cannot Use MS-DOS Core Commands From Shell Function

ID: Q149570

The information in this article applies to:

SYMPTOMS

In the versions of Microsoft Excel listed at the beginning of this article, when you use the Shell function in a Visual Basic for Applications macro to execute an external MS-DOS call, if you use commands contained only in the MS-DOS core, you receive the following error message:

   Invalid Procedure Call

The commands that return this error message are the following:

   DIR, CLS, DEL, CD, MD, RD, MKDIR, RMDIR, COPY

CAUSE

This problem occurs because the Shell function in Microsoft Excel does not start a specific instance of the MS-DOS command interpreter (Command.com). Therefore, the MS-DOS core commands are not present. Only files that have an .exe, .com or .bat extension can be executed through the Shell function.

WORKAROUND

To work around this problem, perform the commands that cause the error message from within Microsoft Excel.

You can perform most of the functions that are provided by Command.com through commands included in Microsoft Excel Visual Basic for Applications. Some common core MS-DOS commands and their Microsoft Excel equivalents are listed below.

MS-DOS command                       Microsoft Excel command
DEL [drive][path]filename            Kill "[drive][path]filename"
CD [drive][path]                     ChDir "[drive][path]"
MD [drive][path]directory name       MkDir "[drive][path]directory name"
RD [drive][path]directory name       RmDir "[drive][path]directory name"
COPY [source] [destination]          FileCopy "[source]","[destination]"
DIR [path]                           Dir("[path]",[attributes])

NOTE: The Visual Basic for Applications Dir command does not function identically to its MS-DOS equivalent, and requires extra code to retrieve an entire directory.

For more information about the Dir command in Visual Basic for Applications, click the Index tab in Microsoft Excel Help, type the following text

   dir

and then double-click the selected text to go to the "Dir function" topic.

In cases where it is absolutely essential to use one of the MS-DOS core commands, it is possible to invoke an instance of the command interpreter manually and pass it the desired command. To do this, use the following syntax:

   a = Shell("command.com /c [command]", [WindowStyle])

NOTE: you must include /c for the command to be executed correctly. For example, to perform a directory command of all hidden files in the root directory, use the following syntax:

   Shell("command.com /c dir c:\ /ah", 1)

MORE INFORMATION

In a normal MS-DOS session when you are using Windows 3.1 or Windows 95, the MS-DOS command interpreter, Command.com, is present to handle core commands. To show that the MS-DOS command interpreter, Command.com, is not in memory, use the following Visual Basic for Applications command:

   a = Shell("MEM /C /P", 1)

Compare this to the output of the MEM command when you run it from an MS- DOS session under Windows or Windows 95. The only difference is that Command.com is not in memory within the environment created by the Visual Basic for Applications Shell function.

Additional query words: 5.00 5.00c 7.00 97 8.00 XL97

Keywords          : kbprg kbdta KbVBA xlwin 
Version           : WINDOWS:5.x,7.0,97
Platform          : WINDOWS

Last Reviewed: December 9, 1998