In Excel and in Access, you receive an "Invalid procedure call" error message when you try to use an MS-DOS core command from the Shell function (149570)



The information in this article applies to:

  • Microsoft Excel for Windows 95
  • Microsoft Excel for Windows 5.0
  • Microsoft Access for Windows 95
  • Microsoft Access 2.0

This article was previously published under Q149570
kbclone: Q213800

SYMPTOMS

In the programs that are listed in the "Applies to" section, when you use the Shell function in a Microsoft Visual Basic for Applications (VBA) 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 VBA 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 by using the equivalent VBA commands.

You can perform most of the functions that are provided by Command.com through commands that are included in VBA. Some common core MS-DOS commands and their VBA equivalents are listed below.
MS-DOS command                       Microsoft VBA 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 or in Microsoft Access 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.

Modification Type:MinorLast Reviewed:10/10/2006
Keywords:kbProgramming KB149570