在VBA中Addressof不能在Class或Form中使用,只有Module模式下才能使用Addressof以面是一个计时器的例子:
Form1中代码:
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As LongPrivate Sub CommandButton1_Click() Module1.testEnd SubPrivate Sub CommandButton2_Click() KillTimer 0, Module1.mHandleEnd Sub
Module1下代码:
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As LongPublic mHandle As LongPublic Sub test() mHandle = SetTimer(0, 0, 2000, AddressOf MyTimerProce)End SubPublic Sub MyTimerProce(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long) MsgBox "hello"End Sub