VB and VBA Users Source Code: Getting the AddressOf a VBA function (Excel 97)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Getting the AddressOf a VBA function (Excel 97)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, September 06, 2000
Hits:
1340
Category:
Office
Article:
Many API functions (especially those concerned with enumeration) require the address of a function in order to perform call backs. However, Excel 97 doesn't directly support this, but fortunately the vba dll does expose an API to get the address of a function within the current project: Option Explicit Private Declare Function GetCurrentVbaProject Lib "vba332.dll" Alias "EbGetExecutingProj" (hProject As Long) As Long Private Declare Function GetFuncID Lib "vba332.dll" Alias "TipGetFunctionId" (ByVal hProject As Long, ByVal strFunctionName As String, ByRef strFunctionID As String) As Long Private Declare Function GetAddr Lib "vba332.dll" Alias "TipGetLpfnOfFunctionId" (ByVal hProject As Long, ByVal strFunctionID As String, ByRef lpfn As Long) As Long 'Purpose : Returns the address of a function in the current project. 'Inputs : sFunctionName Then name of the function 'Outputs : A pointer to the function 'Author : Andrewb 'Date : 06/09/2000 'Notes : MS state that the API Entry point may not be available in future versions. ' Use AddressOf in Excel 2000 'Revisions : Function AddressOf97(sFunctionName As String) As Long Dim lResult As Long, lCurrentVBProject As Long, sFunctionID As String, lAddressOfFunction As Long, sFunctionUniCode As String 'Convert the function name to Unicode sFunctionUniCode = StrConv(sFunctionName, vbUnicode) If GetCurrentVbaProject(lCurrentVBProject) <> 0 Then 'The VBProjects exists 'Get the function ID of the function based on its name lResult = GetFuncID(lCurrentVBProject, sFunctionUniCode, sFunctionID) If lResult = 0 Then 'The function exists 'Get a pointer to the callback function lResult = GetAddr(lCurrentVBProject, sFunctionID, lAddressOfFunction) If lResult = 0 Then 'Return the pointer AddressOf97 = lAddressOfFunction End If End If End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder