每当我用脚本命中错误时,焦点转向VBA代码和违规行。我修复它,并保存。然后我发现脚本不再运行,即使在我确定它没有暂停之后。为什么调试后我的VBA脚本不能继续运行?
例如,我现在使用Form_Timer()
事件来做一些测试(间隔设置为1000ms)。为了再次测试脚本,我将它设置为将来的一分钟(例如,如果当前时间是上午8:54:00,我将它设置为在上午8:55:00启动)。但是在错误发生后这会停止工作。有人知道为什么吗?我不想告诉我的用户关闭并重新打开Access数据库的副本,只是为了让脚本再次运行。
代码:
Private Sub Form_Timer()
On Error GoTo ErrorHandler
current_date_time = Now
If current_date_time = #6/28/2016 8:52:00 AM# Then
MsgBox ("the current_date_time variable holds: " & current_date_time)
'Declare objects
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
Dim mail_body As String
'Set objects
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qry_BMBFLoc")
Set rst = qdf.OpenRecordset
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
mail_body = "The following jobs do not have the special BF location set in Job Orders: " & vbCrLf
If Not (rst.EOF And rst.BOF) Then
rst.MoveFirst
Do Until rst.EOF = True
mail_body = mail_body & rst!job & "-" & rst!suffix & vbCrLf
rst.MoveNext
Loop
'Email contents
oMail.Body = mail_body
oMail.Subject = "Blah"
oMail.To = "[email protected]"
oMail.Send
'Close stuff
rst.Close
dbs.Close
Set rst = Nothing
Set oMail = Nothing
Set oApp = Nothing
End If
End If
Exit Sub
ErrorHandler:
Dim msg As String
If Err.Number <> 0 Then
msg = "email Form Timer Error #" & Str(Err.Number) & " error Line: " & Erl & Chr(13) & Err.Description
MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Exit Sub
End Sub
这可能是一个愚蠢的问题,但在你调试你的宏后,你打f5继续脚本? – jellz77
@ jellz77不,我没有。在我打F5后,它弹出一个对话框,要求输入一个宏名称,但我不知道要给它一个宏。我正在运行VBA脚本... – whatwhatwhat
我调试后,确保脚本没有按下Ctrl + S(保存)来暂停。它删除顶部的'[break]',所以我认为脚本恢复了。但它不再发射。 – whatwhatwhat