Shell Function


Runs an executable program.


Shell(commandstring [, windowstyle])


The Shell function has these parts:

Part                        Description


commandstring        Name of the program to execute and any required arguments or command line switches.  If the program name in commandstring doesn't include a .COM, .EXE, .BAT, or .PIF file extension, .EXE is assumed.

windowstyle             Number corresponding to the style of the window in which the program is to be executed.  If windowstyle is omitted, the program is opened minimized with focus, the same as windowstyle = 2.

The following table identifies the values for windowstyle and the resulting style of window:

Value                      Window style


1, 5, 9                      Normal with focus.

2                             Minimized with focus (default).

3                             Maximized with focus.

4, 8                          Normal without focus.

6, 7                          Minimized without focus.


If the Shell function successfully executes the named program, it returns the instance handle of the started program.  The instance handle is a token identifying the running program.  If the Shell function can't start the named program, an error occurs.


Note   The Shell function runs other programs asynchronously.  That means that you cannot depend on a program started with Shell to be finished executing before the code following the Shell function in your Visual Basic application is executed.


Shell Function Example

The example uses Shell to leave the current application and run the Calculator program included with Microsoft Windows; it then uses the SendKeys statement to send keystrokes to either quit the Calculator or add some numbers, depending on the user's choice.  AppActivate is used to move the focus between Microsoft Visual Basic and the Calculator.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim I, X, Msg                         ' Declare variables.

   AppActivate "Microsoft Visual Basic"  ' Change focus to Visual Basic.

   SendKeys "%{  }{ Down 3}{ Enter}", True  ' Minimize Visual Basic.

   X = Shell("Calc.exe", 1)              ' Shell Calculator.

   For I = 1 To 100                      ' Set up counting loop.

      SendKeys I & "{ +}", True           ' Send keystrokes to Calculator

   Next I                                ' to add each value of I.

   SendKeys "=", True                    ' Get grand total.

   AppActivate "Microsoft Visual Basic"  ' Return focus to Visual Basic.

   Msg = "Choose OK to close the Calculator.

   MsgBox Msg                            ' Display OK prompt.

   AppActivate "Calculator"              ' Return focus to Calculator.

   SendKeys "%{ F4}", True                ' Alt+F4 to close Calculator.

   AppActivate "Microsoft Visual Basic"  ' Return focus to Visual Basic.

   SendKeys "%{  }{ Enter}", True          ' Restore Visual Basic to size.

End Sub