How to deal with “runtime error 50290” when using SetTimer API?

一世执手 提交于 2019-12-13 19:50:37

问题


I've run into this error when trying to make a stopwatch timer in Excel. Here's a simple test code. Create an empty Excel workbook with a button. And assign a macro to it:

Sub Button1_Click()
    TimerID = SetTimer(0&, 0&, 0.5 * 1000&, AddressOf TimerProc)
End Sub

Also, add this code to the module:

Declare Function SetTimer Lib "user32" ( _
    ByVal HWnd As Long, _
    ByVal nIDEvent As Long, _
    ByVal uElapse As Long, _
    ByVal lpTimerFunc As Long) As Long

Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal dwTimer As Long)
    Range("A1") = "test"
End Sub

Then click the button and start clicking random cells. Shortly you will get the following window:

And after that Excel crashes.

What am I doing wrong?
How to handle this?


回答1:


I was able to solve it with

On Error Resume Next

at the beginning of TimerProc. Some more (in Russian) or less related links.

Or probably even better:

Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal dwTimer As Long)
    On Error GoTo BeforeExit
    Range("A1") = "test"
BeforeExit:
End Sub


来源:https://stackoverflow.com/questions/32669344/how-to-deal-with-runtime-error-50290-when-using-settimer-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!