Shell Function

Named Arguments

Yes

Syntax

Shell(pathname[,windowstyle])


pathname

Use: Required

Data Type: Variant (String)

Name of the program to execute.


windowstyle

Use: Optional

Data Type: Variant (Integer)

The style of window and whether it receives the focus.

Return Value

A Variant of subtype Double.

Description

Launches another application and, if successful, returns that application's task ID.

Rules at a Glance

  • pathname can include a drive letter. If a drive letter isn't included in pathname, the current drive is assumed. If a drive letter is used, the fully qualified path is required unless the file is located in the current directory.

  • pathname can include a folder name. If the folder name isn't included in pathname, the current folder is assumed.

  • pathnamecan include any command-line arguments and switches required by the application.

  • Visual Basic includes the following intrinsic constants for setting the windowstyle argument:


vbHide

Value: 0

New application window is: hidden

Focus: New Application


vbNormalFocus

Value: 1

New application window is: shown in its original position and size

Focus: New Application


vbMinimizedFocus

Value: 2

New application window is: displayed as an icon

Focus: New Application


vbMaximizedFocus

Value: 3

New application window is: maximized

Focus: New Application


vbNormalNoFocus

Value: 4

New application window is: shown in its original position and size

Focus: Current Application


vbMinimizedNoFocus

Value: 6

New application window is: displayed as an icon

Focus: Current Application

  • The default when no windowstyle is specified as vbMinimizedFocus (2).

  • If the application named in pathname executes successfully, Shell returns the window's task ID of the program. (The task ID is better known as the process ID (or PID), a unique 32-bit value used to identify each running process.) It can be used as a parameter to the AppActivate statement to give the application the focus and possibly control it remotely using the SendKeys statement. The process ID is also required by a number of Win32 API calls.

  • If the application named in pathname fails to execute, a runtime error is generated.

  • The file launched by Shell must be executable. That is, it must be a file whose extension is .EXE or .COM (an executable file), .BAT (a batch file), or .PIF (a DOS shortcut file).

Programming Tips and Gotchas

  • Applications launched by the Shell function run asynchronously, which means that the launching application isn't notified when the launched application has finished executing. Therefore, program statements in the launching application may execute before the launched application has completed.

  • If you require the Shell function to run another program synchronously, you need to use Win32 API calls. The required calls vary depending upon whether you are using Windows 95 or Windows NT. Be aware, though, that the programming required to run another process in Windows NT is quite complex, and I could quite easily take up another book trying to explain it. In fact, certain procedures relating to launching processes in Windows NT aren't possible using VB—or for that matter MFC. In 16-bit Windows, launching applications or processes synchronously and keeping track of them was a relatively painless exercise; you'd simply monitor the GetModuleUseage API call. However, 32-bit Windows operates on an entirely different system of launching and monitoring (or not, as the case may be) processes, which doesn't allow you to use GetModuleUseage.

  • The Shell function doesn't use file associations. You can't, for example, supply MyReport.Doc as the pathname in the hope that VB will load Microsoft Word, which in turn will load MyReport.Doc.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset