Moving between Applications

Because the user can work simultaneously with several applications in the Windows environment, your VBA procedure must know how to switch between the open programs. Suppose that in addition to Microsoft Excel, you have two other applications open: Microsoft Word and Windows Explorer. To activate an already open program, use the AppActivate statement using the following syntax:

AppActivate title [, wait]

Only the title argument is required. This is the name of the application as it appears in the title bar of the active application window or its task ID number as returned by the Shell function.

Note: The application title is compared to the title string of each running application. If there is no exact match, any application whose title string begins with title is activated.

The second argument, wait, is optional. This is a Boolean value (True or False) that specifies when Visual Basic activates the application. The value of False in this position immediately activates the specified application, even if the calling application does not have the focus. If you place True in the position of the wait argument, the calling application waits until it has the focus. Then it activates the specified application.

For example, to activate Microsoft Word, you should enter the following statement:

AppActivate "Microsoft Word"

Notice that the name of the application is surrounded by double quotation marks.

You can also use the return value of the Shell function as the argument of the AppActivate statement:

' run Microsoft Word

ReturnValue = Shell("C:\Microsoft Office\Office\Word.exe",1) ' activate Microsoft Word AppActivate ReturnValue

The AppActivate statement is used for moving between applications and requires that the program is already running. This statement merely changes the focus. The specified application becomes the active window. The AppActivate statement will not start an application running. For an example of using this statement, see the FindCPLFiles procedure in the next section.

Let's practice some of these recently introduced VBA statements:

1. Open Windows Explorer by typing in the Immediate window the following statement:

Shell "Explorer"

After you press Enter, the requested application is opened. On the Windows taskbar the icon with the My Documents folder should appear.

2. Enter the following statement in the Immediate window: AppActivate "My Documents"

After pressing Enter, the focus moves to the My Documents window.

0 0

Post a comment