Microsoft Office 2000/Visual Basic Programmer's Guide   

Why Use VBA to Call the Windows API?

VBA is a powerful tool for building Windows applications. With VBA, however, you have control over only a small portion of the operating system, the part that is available through the functions and objects exposed directly to VBA in your application. The Windows API includes functions to control the most minute aspects of the operating system. You can extend and fine-tune your VBA applications by calling functions in the Windows API from VBA.

For example, VBA provides functions that read from and write to a portion of the registry reserved for VBA applications. You can use these functions — GetSetting, GetAllSettings, SaveSetting, and DeleteSetting — to maintain information about your application between sessions. However, these functions permit you to work with only one subkey in the registry, namely the \HKEY_CURRENT_USER\Software\VB and VBA Program Settings subkey. If you want to store or retrieve information from any other part of the registry, you must use the Windows API.

Because the Windows API provides such granular control over the operating system, you may need to call several functions to accomplish a single task. There are several different API functions for manipulating the registry, which you use in conjunction with each other. For example, to assign a new value to a registry subkey, you can use the RegOpenKeyEx function, which opens an existing subkey for reading or writing values. Then you can use the RegSetValueEx function to set the new value for that subkey, followed by the RegCloseKey function to close the subkey.

The API functions for manipulating the Windows Clipboard may also be useful to you as a VBA programmer. VBA doesn't provide an object for working with the Clipboard, but you can create your own by wrapping the Clipboard API functions in a class module to create a simple, reusable object representing the Windows Clipboard. You may need to use several functions together to accomplish a single task. For example, the OpenClipboard function opens the Clipboard for examination and prevents other applications from modifying the Clipboard content. The GetClipboardData function returns data that's saved to the Clipboard, and the CloseClipboard function closes the Clipboard, making it again available to other applications.

Note   Each of the Office 2000 applications provides a Clipboard toolbar that you can use to store up to 12 items. You can also work with the items stored by the Clipboard toolbar programmatically through the command bar object model. The last item copied to the Clipboard toolbar is the one stored in the Windows Clipboard.

Other useful API functions are demonstrated in the sample applications referred to in this chapter. The IniFile.xls sample demonstrates how to read from and write to an initialization (.ini) file.

The System.xls sample contains a custom class named System, which has a number of properties that set and return general information about the operating system. For example, you can determine the current display resolution. You can set and retrieve the local system time, and return the paths to the Windows folder and the Windows temporary folder. You can expand this sample to provide further information about the system.

The EnumWindows.xls sample contains code for working with windows. This sample creates a collection of custom objects representing application windows that are open and visible in the operating system. You can use the API functions for working with windows to determine which applications are currently running and which one is currently active. You can set or retrieve the size of a window, return its caption, make its caption flash, make it the active window, minimize the window, and so on.

These sample files are available in the ODETools\V9\Samples\OPG\Samples\CH10 subfolder on the Office 2000 Developer CD-ROM.