Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleStart another program using Shell and get its task ID
Description
KeywordsShell, start, execute, task ID
CategoriesWindows
 
The Shell function starts a new program and returns the new process's task ID or zero if it cannot execute the program. The following code calls Shell and displays the result in the lblTaskId Label.
 
' Start the program.
Private Sub cmdRun_Click()
    On Error GoTo ShellError

    Select Case cboShellStyle.Text
        Case "vbHide"
            lblTaskId.Caption = Hex$(Shell(txtProgram.Text, _
                vbHide))
        Case "vbMaximizedFocus"
            lblTaskId.Caption = Hex$(Shell(txtProgram.Text, _
                vbMaximizedFocus))
        Case "vbMinimizedFocus"
            lblTaskId.Caption = Hex$(Shell(txtProgram.Text, _
                vbMinimizedFocus))
        Case "vbMinimizedNoFocus"
            lblTaskId.Caption = Hex$(Shell(txtProgram.Text, _
                vbMinimizedNoFocus))
        Case "vbNormalFocus"
            lblTaskId.Caption = Hex$(Shell(txtProgram.Text, _
                vbNormalFocus))
        Case "vbNormalNoFocus"
            lblTaskId.Caption = Hex$(Shell(txtProgram.Text, _
                vbNormalNoFocus))
    End Select
    Exit Sub

ShellError:
    MsgBox "Error Shelling file." & vbCrLf & _
        Err.Description, vbOKOnly Or vbExclamation, _
        "Error"
    Exit Sub
End Sub
 
Note that in VBA with Access 2002, Shell() no longer returns a task handle as described in examples and the Access 2002 documentation, but rather returns True or False. Go figure.

See also:

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated