Lotus 脚本:如何修复错误“91 未在 FUNCTIONNAME 中设置对象变量(未设置对象变量)”

Lotus script: How to fix error "91 Object variable not set in FUNCTIONNAME(Object variable not set)"

代码在 运行 手动(右键单击和 运行)时运行完美,但在使用 schedule 自动执行时会出现问题。 自动化代码 运行 没问题,但就在 运行 结束代码时,它失败并显示上述错误消息。

代码看起来不错,变量设置正确,手动完成时代码运行没问题。

Sub processJobs(dbCurrent As NotesDatabase)
Dim vwLookup As NotesView
Dim docReq As NotesDocument
Dim dtMonthAgo As New NotesDateTime(Today)
Dim dtDelDate As NotesDateTime
Dim itmDelDate As NotesItem
Dim sender As NotesName
Dim receiver As NotesName
Dim nmServer As NotesName
Dim lngNoOfDays As Long
Dim mail As Email
Dim intCount As Integer
Dim intCountFailed As Integer
Dim strSendTo As String

On Error GoTo ErrorHandler
On Error 4000 GoTo RecipientNameBlank
On Error 4294 GoTo RecipientNotInNAB

Call AgentLog.LogAction("--------- Process Job ---------")

Call dtMonthAgo.AdjustMonth( -1 )  ' set the dtMonthAgo date to one month ago
Call dtMonthAgo.Setanytime() ' remove the time component from the date

Set vwLookup = dbCurrent.Getview("JobView")
vwLookup.Autoupdate = False

Set docReq = vwLookup.Getfirstdocument()

intCount = 0
intCountFailed = 0
Do Until docReq Is Nothing
    Set itmDelDate = docReq.GetFirstItem("DeliveryDate")
    If itmDelDate.Type = 1024 Then
        Set dtDelDate = itmDelDate.DateTimeValue
        Call dtDelDate.SetAnyTime

        If dtMonthAgo.TimeDifference(dtDelDate) > 0  Then
            intCount = intCount + 1
            Set mail = New Email ' send email...
            mail.Subject = "Processed Job"
            mail.HTML = getCompletionHTML(docReq, mail.WebURL)

            Set sender = New NotesName(docReq.JobBy(0))
            Set receiver = New NotesName(docReq.DespatchTo(0))
            Set nmServer = New NotesName(dbCurrent.Server)
            If receiver.Organization = nmServer.Organization Then
                strSendTo = receiver.Abbreviated
                ' send a copy to..
                If sender.Abbreviated <> receiver.Abbreviated Then
                    mail.CopyTo = docReq.JobBy(0)
                End If
            Else
                strSendTo = sender.Abbreviated
            End If

            mail.Send(strSendTo)
            Call agentLog.LogAction(strSendTo & " - Job No: " & docReq.JobNo(0))
   flagDoc:
            ' flag the job...
            Call docReq.Replaceitemvalue("CompletionJob", "Y")
            Call docReq.Replaceitemvalue("CompletionJobDate", Now)
            Call docReq.Save(True, False)
        End If
    End If
    Set docReq = vwLookup.Getnextdocument(docReq)
Loop

Call AgentLog.LogAction("")
Call AgentLog.LogAction("Attempted to send " & CStr(intCount) & " Job")
Call AgentLog.LogAction("Failed to send " & CStr(intCountFailed) & " Job")
Call AgentLog.LogAction("--------- End of job process ---------")

ErrorHandler:
If Not AgentLog Is Nothing Then
    Call AgentLog.LogError(Err, "errorHandler: " & CStr(Err) & " " & Error$ & " in " & LSI_Info(2))
End If
Resume getOut

23/05/2019 00:00:05 errorHandler: 91 Object variable not set in PROCESSJOBS(Object variable not set)

代理应该遍历视图,获取收件人姓名,设置变量,然后自动发送电子邮件。 通过自动化,它确实循环遍历收件人的视图和 get/set 名称,但在获取未设置对象变量的姓氏后直接失败。 运行 手动代码根本不会造成任何问题,但此代码需要 运行 自动。

在您的 ErrorHandler 中,记录(或打印)发生错误的行。

ErrHandler:
Print "Got error " & Error$ & " on line " & cstr(Erl)

示例复制自 IBM

您需要一个 Exit Sub 语句来防止您的代码进入错误处理程序。

Call AgentLog.LogAction("")
Call AgentLog.LogAction("Attempted to send " & CStr(intCount) & " Job")
Call AgentLog.LogAction("Failed to send " & CStr(intCountFailed) & " Job")
Call AgentLog.LogAction("--------- End of job process ---------")

Exit Sub ' **** You need this

ErrorHandler:
If Not AgentLog Is Nothing Then
    Call AgentLog.LogError(Err, "errorHandler: " & CStr(Err) & " " & Error$ & " in " & LSI_Info(2))
End If
Resume getOut

您似乎也没有初始化 AgentLog,尽管这可能是 global.Is 当您 运行 它安排时它成功地将这些行写入代理日志?如果没有,可能是访问预定服务器上的代理日志数据库时出现问题。