Microsoft Office 2000/Visual Basic Programmer's Guide   

Calling a Callback Function

In previous versions of VBA, it is not possible to use callback functions, because there is no way to tell the DLL function which of your own functions you want to call. In order to call a callback function, a DLL function needs a pointer to the callback function's address in memory. Previous versions of VBA do not support pointers to functions. VBA now supports the AddressOf operator, which enables you to pass the address of a VBA function to a DLL.

The Class_Initialize procedure in the ParentWindows collection calls the EnumWindows function, passing the address of EnumWindowsProc for the lpEnumFunc argument, and a reference to the ParentWindow object itself (using the Me keyword) for the lParam argument. Note that the AddressOf operator is followed by the name of the callback function, without any arguments:

Private Sub Class_Initialize()
      
   ' Create new instance of private collection object.
   Set mcolParents = New Collection
   
   ' Add visible parent windows to collection.
   ' Pass Me as reference to ParentWindows collection.
   EnumWindows AddressOf EnumWindowsProc, Me
End Sub

The ParentWindows Class_Initialize event procedure is available in the ParentWindows class module in EnumWindows.xls in the ODETools\V9\Samples\OPG\Samples\CH10 subfolder on the Office 2000 Developer CD-ROM.