Programming with the Windows API

Visual Basic for Applications is a high-level language that provides you with a rich, powerful, yet quite simple set of functionality for controlling the Office suite of products, as well as many other applications. You are insulated — some would say protected — from the "mundane minutiae" of Windows programming that, say, a C++ programmer has to contend with.

The price you pay for this protection is an inability to investigate and control many elements of the Windows platform. You can, for example, use Application.International to read most of the Windows Regional Settings and read the screen dimensions from Application.Usable Width and Application.UsableHeight, but that's about it. All the Windows-related items available to you are properties of the Application object and are listed in Appendix A.

The Windows platform includes a vast amount of low-level functionality that is not normally accessible from VBA, from identifying the system colors to creating a temporary file. Some of the functionality has been exposed in VBA, but only to a limited extent, such as creating and using an Internet connection (for example, you can open a page from the Internet using Workbooks.Open "<URL>", but you can't just download it to disk). There are also a number of other object libraries typically available on Windows computers that provide high-level, VBA-friendly access to the underlying Windows functionality. Examples of these are the Windows Scripting Runtime and the Internet Transfer Control.

There are times, though, when you need to go beyond the limits of VBA and the other object libraries, and delve into the files that contain the low-level procedures provided and used by Windows. The Windows Operating System is made up of a large number of separate files, mostly dynamic link libraries (DLLs), each containing code to perform a discrete set of interrelated functions. DLLs are files that contain functions that can be called by other Windows programs or other DLLs. They cannot be run like programs themselves.

These files are collectively known as the Windows Application Programming Interface, or Windows API. Some of the most common files you'll use in the Windows API are detailed in the following table.

File

Function Group(s)

USER32.EXE

User interface functions (such as managing windows, the keyboard, clipboard, and so on)

KERNEL32.DLL

File- and system-related functions (such as managing programs)

GDI32.DLL

Graphics and display functions

SHELL32.DLL

Windows shell functions (such as handling icons and launching programs)

COMDLG32.DLL

Standard Windows dialog functions

ADVAPI32.DLL

Registry and NT Security functions

MPR.DLL and NETAPI32.DLL

Network functions

WININET.DLL

Internet functions

WINMM.DLL

Multimedia functions

WINSPOOL.DRV

Printing functions

This chapter explains how to use the functions contained in these files in your VBA applications and includes a number of useful examples. All of the Windows API functions are documented in the Platform SDK section of the MSDN Library at http://msdn.microsoft.com/library/ default.asp?url=/library/en-us/winprog/winprog/windows_api_start_page.asp, which can be thought of as the online help for the Windows API. Because Microsoft regularly changes its URLs, that page can be found in the MSDN Library menu under Win32 and COM Development O Development Guides O Windows API O Windows API.

+2 -1

Post a comment