Re: Addressof and VBA

From: ALESSANDRO Baraldi (ik2zok_at_libero.it)
Date: 08/10/04


Date: Tue, 10 Aug 2004 21:24:16 +0200


"ssmgr" <ssmgr@discussions.microsoft.com> ha scritto nel messaggio
news:44E7A1C7-F4C3-4BD8-84F2-FE5E704EAB52@microsoft.com...
> Is the Addressof operator available in VBA? I'm using NT4 and Office97,
and
> wish to write a callback function for the EnumWindows function. VBA
doesn't
> recognise the Addressof operator, so am I dead in the water? Please
advise.
>
> --
> Steve

On Office97 you need a Emulation of Addressof.
>From http://www.mvps.org/access/api/api0031.htm

'------------------------------------------------------------------------
Option Compare Database
Option Explicit

'---------------------------------------------------------------------------
----------------------------------------
' Declarations
'
' These function names were puzzled out by using DUMPBIN /exports
' with VBA332.DLL and then puzzling out parameter names and types
' through a lot of trial and error and over 100 IPFs in MSACCESS.EXE
' and VBA332.DLL.
'
' These parameters may not be named properly but seem to be correct in
' light of the function names and what each parameter does.
'
' EbGetExecutingProj: Gives you a handle to the current VBA project
' TipGetFunctionId: Gives you a function ID given a function name
' TipGetLpfnOfFunctionId: Gives you a pointer a function given its
function ID
'
'---------------------------------------------------------------------------
----------------------------------------
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

'---------------------------------------------------------------------------
----------------------------------------
' AddrOf
'
' Returns a function pointer of a VBA public function given its name. This
function
' gives similar functionality to VBA as VB5 has with the AddressOf param
type.
'
' NOTE: This function only seems to work if the proc you are trying to get
a pointer
' to is in the current project. This makes sense, since we are using a
function
' named EbGetExecutingProj.
'---------------------------------------------------------------------------
----------------------------------------
Public Function AddrOf(strFuncName As String) As Long
    Dim hProject As Long
    Dim lngResult As Long
    Dim strID As String
    Dim lpfn As Long
    Dim strFuncNameUnicode As String

    Const NO_ERROR = 0

    ' The function name must be in Unicode, so convert it.
    strFuncNameUnicode = StrConv(strFuncName, vbUnicode)

    ' Get the current VBA project
    ' The results of GetCurrentVBAProject seemed inconsistent, in our tests,
    ' so now we just check the project handle when the function returns.
    Call GetCurrentVbaProject(hProject)

    ' Make sure we got a project handle... we always should, but you never
know!
    If hProject <> 0 Then
        ' Get the VBA function ID (whatever that is!)
        lngResult = GetFuncID( _
         hProject, strFuncNameUnicode, strID)

        ' We have to check this because we GPF if we try to get a function
pointer
        ' of a non-existent function.
        If lngResult = NO_ERROR Then
            ' Get the function pointer.
            lngResult = GetAddr(hProject, strID, lpfn)

            If lngResult = NO_ERROR Then
                AddrOf = lpfn
            End If
        End If
    End If
End Function

'---------------------------------------------------------------------------
---------



Relevant Pages