Anatomy of an API Call

Before you can use the procedures contained in the Windows DLLs, you need to tell the VBA interpreter where they can be found, the parameters they take, and what they return. Do this using the Declare statement, which VBA help shows as:

[Public | Private] Declare Sub name Lib "libname" [Alias "aliasname"] [([arglist])]

[Public | Private] Declare Function name Lib "libname" [Alias " aliasname"] [([arglist])] [As type]

The following is the declaration used to find the Windows TEMP directory:

Private Declare Function GetTempPath Lib "kernel32" _ Alias "GetTempPathA" ( _ ByVal nBufferLength As Long, _ ByVal lpBuffer As String) As Long

This tells VBA that:

□ The function is going to be referred to in the code as GetTempPath

□ The procedure can be found in kernel32.dll

□ It goes by the name of GetTempPathA in the DLL (case sensitive)

□ It takes two parameters, a Long and a String (more about these later)

Microsoft used to include a simple API declaration viewer with the Developer Editions of Office, but that is now only available by installing Visual Studio and hasn't been updated to include the more recent versions of Windows. At the time of this writing, a great alternative is available for free download from www.activevb.de/rubriken/apiviewer/index-apiviewereng.html.

0 0

Post a comment