There is no way to implement a callback function in VBA wihout creating an API in C (Dynamic Library Link).
Create Win32 Dynamic Library Link Project with VC++
- Start VC++ IDE ,
- Select New from File menu. Then select Win32 Dynamic - link library from Projects tab.
- Enter project name as "CallBackFuncApi"
- then click OK button, Now you can see a dialog box with caption Win32 Dynamic - Link library - step 1 of 1
- Select An Empty DLL project , and click Finish.
- Create the follwing files cb.h , cb.cpp and cb.def
#define EXTERNC extern "C"
#define DLLAPI __declspec(dllexport)
#define WINAPI __stdcall
typedef bool (CALLBACK *CallbackFunc) (VARIANT userData);
EXTERNC DLLAPI DWORD WINAPI CallBackFuncVariantHndl(CallbackFunc pCallback);
/*** cb.cpp ***/
#include <windows.h>
#include "cb.h"
EXTERNC DLLAPI void WINAPI CallBackFuncVariantHndl(CallbackFunc pCallback, VARIANT userData)
{
(*pCallback)(userData);
}
}
/*** cb.def ***/
EXPORTS
CallBackFuncVariantHndl
Using CallBack function api with VBA
You have to copy CallBackFuncApi.Dll into system32 folder
Public Declare Function CallBackFuncVariantHndl Lib "CallBackFuncApi" (ByVal pCallback As Long) As Long
'Call back function with variant parameter (should be in Module)
Public Sub MyCallBackFunction(ByVal vnt As Variant)
' Enter your code
End Sub
'
Public Sub MyCallBackFunction(ByVal vnt As Variant)
Dim vnt As Variant
vnt = split("Item 1 - Item 2 - Item 3 ","-")
Call CallBackFuncVariantHndl (AddressOf MyCallBackFunction, vnt)
End Sub
 
 
2 comments:
Here is a pretty cool example from Chris West: http://gotochriswest.com/blog/2011/07/18/vbscript-regexp-replace-using-a-callback-function/
It shows how you can have a replace function in VBScript which uses a callback function like the one that is available in JavaScript.
thank you for your reply. I think that we can't use Chris west's example with VBA because GetRef function is not supported.
Post a Comment