lostfalling
2019-06-30 14:01
采纳率: 68.4%
浏览 490

vba 当触发KillTimer时,excel直接闪退了,连看代码出错的的时间都没有,敢问大神原因?


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

Public Declare PtrSafe Function KillTimer Lib "user32" ( _
    ByVal HWnd As Long, _
    ByVal nIDEvent As LongPtr) As Long

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public TimerID As Long
Public mymy As Integer
Sub StartTimer()
    TimerID = SetTimer(0&, 0&, 50&, AddressOf TimerProc)
    DoEvents
End Sub

Sub EndTimer()'触直接excel闪退
    On Error Resume Next
    KillTimer 0&, TimerID
End Sub
Sub TimerProc()
    mymy = mymy + 1
    Sheet1.Cells(5, 5) = mymy
End Sub
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • blownewbee 2019-06-30 16:36
    已采纳

    参考:
    http://www.exceloffice.net/archives/1245

    不要在vba/vb里使用带有回调函数的api(比如你这个addressof TimerProc),因为vba/vb缺乏异常处理机制,在回调中的异常会导致整个程序崩溃

    点赞 打赏 评论

相关推荐 更多相似问题