With this code you can Call a function by name.
You need to get the vba332.dll that in my case followed Office97. I knew I would find a way to do CallByName and I did it. Just thought I would share it to whomever needs it.
The AddrOf function was made by someone else who made it to be able to emulate the AddressOf from VB5 in VBA.
Code:Option Explicit Private Declare Function SendMessageCallback Lib "user32" Alias "SendMessageCallbackA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal lpResultCallBack As Long, ByVal dwData As Long) As Long Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long 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 Function SubTest(ByVal hwnd As Long, ByVal umsg As Integer, dwData As Long, lresult As Long) As Long MsgBox "CallByName worked." SubTest = True End Function Sub CallByNameTest() Dim hWndd As Long 'Windows handle hWndd = FindWindow(CLng(0), CStr(ActiveDocument.Parent.Caption)) 'Windows handle in VBA SendMessageCallback hWndd, 0, 0, 0, AddrOf("SubTest"), 0 End Sub Private Function AddrOf(strFuncName As String) As Long Dim hProject As Long, lngResult As Long, strID As String, lpfn As Long, strFuncNameUnicode As String Const NO_ERROR = 0 strFuncNameUnicode = StrConv(strFuncName, vbUnicode) Call GetCurrentVbaProject(hProject) If hProject <> 0 Then lngResult = GetFuncID( _ hProject, strFuncNameUnicode, strID) If lngResult = NO_ERROR Then lngResult = GetAddr(hProject, strID, lpfn) If lngResult = NO_ERROR Then AddrOf = lpfn End If End If End Function


Reply With Quote