none
SetTimer in Excel 2010 RRS feed

  • Question

  • Hi,

    I had a VBA script running in Office 2007/32 bit without any problems, now I switched to the Office 2010/64-bit.

    In Office 2010/64-bit the TimerProc (MyTimerProc) passed to SetTimer is never called. Why?

    This is the script that was running in Office 2007 (it shows a Message Box every 10 seconds):

    Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Private mHandle As Long
    Private Sub CommandButton1_Click()
        mHandle = SetTimer(0, 0, 10000, AddressOf MyTimerProc)
    End Sub
    Private Sub CommandButton2_Click()
        KillTimer 0, mHandle
    End Sub
    ---
    Public Sub MyTimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
        MsgBox "hello"
    End Sub

    In 2010 it looks more or less the same, just with some LongPtrs instead of Longs:

    Private Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
    Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
    Private mHandle As LongPtr
    Private Sub CommandButton1_Click()
        mHandle = SetTimer(0, 0, 10000, AddressOf MyTimerProc)
    End Sub
    Private Sub CommandButton2_Click()
        KillTimer 0, mHandle
    End Sub
    ---
    Public Sub MyTimerProc(ByVal hwnd As LongPtr, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
        MsgBox "hello"
    End Sub



    • Edited by michivo Monday, January 14, 2013 10:06 AM problem was not caused by 64 bit but excel 2010
    Friday, January 11, 2013 12:07 PM

Answers

  • Hi michivo.

    Thanks for posting in the MSDN Forum.

    It's based on my experience that your code will not wok either in Excel 2010 32-bit or Excel 2010 64-bit. Addressof operator will invalid in the Form class. You need call them at a module.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by michivo Monday, January 14, 2013 8:24 AM
    Monday, January 14, 2013 2:39 AM
    Moderator

All replies

  • Hi michivo.

    Thanks for posting in the MSDN Forum.

    It's based on my experience that your code will not wok either in Excel 2010 32-bit or Excel 2010 64-bit. Addressof operator will invalid in the Form class. You need call them at a module.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by michivo Monday, January 14, 2013 8:24 AM
    Monday, January 14, 2013 2:39 AM
    Moderator
  • Hi,

    I tried the above code, it is not working well. Could you please tell the correct code for using the this timer.

    Thanks,

    Kenyon

    Tuesday, January 3, 2017 12:13 AM