Office 2010下VBA Addressof的应用

在VBA中Addressof不能在Class或Form中使用,只有Module模式下才能使用Addressof以面是一个计时器的例子:

Form1中代码:

Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Private Sub CommandButton1_Click()
    Module1.test
End Sub
Private Sub CommandButton2_Click()
    KillTimer 0, Module1.mHandle
End 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 Long
Public mHandle As Long

Public Sub test()
    mHandle = SetTimer(0, 0, 2000, AddressOf MyTimerProce)
End Sub


Public Sub MyTimerProce(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    MsgBox "hello"
End Sub


你可能感兴趣的:(Office)