Access Cookbook - Ken Getz [236]
TIP
If you're using Windows 98 or Windows Me, you'll need to take into consideration file and path names that include embedded spaces. Windows 2000, Windows XP, and later operating systems handle spaces in file names without any trouble. For earlier operating systems, make sure you surround file and path names that include spaces with quote marks (Chr$(34)) in order to ensure proper handling when you call the ShellExecute API function.
11.8. Check to See if an Application Is Already Running
Problem
You need to start up other Windows programs from within your Access application—for instance, to send data to Excel or to format a report in Word. If you just use the Shell command to start these programs, you may end up with multiple instances of the application. How can you tell if an application is already running before you attempt to start it?
Solution
There are a number of solutions to this problem, and none, unfortunately, are as easy as you might like. To ask Windows whether Excel is currently running and receive an answer, you must know the Windows class name for the main window of the application. This solution explains the format of the question and how to ask it. In addition, it demonstrates how to switch to a running application from your Access application.
If you have code that interacts with applications external to Access, it is often useful to be able to determine whether the application is running. The sample form, frmAppsRunning (Figure 11-10), asks Windows the question, "Is this app running?" for each of six predefined window classes, and you can add one more of your own. For each application that frmAppsRunning finds, it fills in the window handle (hWnd) column and the window caption column on the form. The AppActivate command in Access requires that you know the exact title of the window, so this form uses code from Chapter 4 (in basAccessCaption) to retrieve the caption for each running application. Finally, you can click on any of the enabled buttons in the righthand column to switch to the running application.
Figure 11-10. frmAppsRunning shows the state of certain applications
Try the sample form with Microsoft applications you have installed. Press F1 to bring up Help, and then switch back to Access and click on the Search button on the sample form. This will reinitiate the search for active applications, and it will find WINHELP.EXE running. Click on the question-mark icon to switch back to WinHelp.
Follow these steps to include this functionality in your own applications:
Import the modules listed in Table 11-9 from 11-08.MDB into your application.
Table 11-9. Modules to import from 11-08.MDB
Module
Contains
basCaption
acbGetAccessCaption, acbGetWindowCaption, acbSetAccessCaption
basUsage
acbIsAppLoaded
To be able to ask Windows the question, "Is some application running?", you'll need to know the Windows class name for the main window of the application. Table 11-10 lists the names for several Windows applications.
Table 11-10. Windows application class names
Application
Class name
Access (all versions)
OMain
Excel (all versions)
XLMain
Explorer
ExploreWClass
Outlook (all versions)
rrctrl_renwnd32
Notepad
Notepad
PowerPoint 2003
PP11FrameClass
WordPad
WordPadClass
Microsoft Word (all versions)
OpusApp
The class names are somewhat arbitrary. Because they're assigned by the development staff, not by the marketing or documentation departments, class names often reflect the project's code name or the state of mind of the developer.
FINDING CLASS NAMES
There are many ways to find the class names for applications' main windows. The simplest is to use the sample form for the Solution in Recipe 11.9, which displays a list of open windows and their class names. If you want to know the