在莲花脚本中的getfirstitem之后无法退出循环

时间:2016-02-03 07:24:42

标签: lotus-notes lotusscript

Sub Initialize

On Error GoTo ErrorOut  

Dim sess As NotesSession
Dim db As NotesDatabase
Dim doc, searchDoc, reqNumDoc As NotesDocument
Dim body As NotesMIMEEntity
Dim header As NotesMIMEHeader
Dim stream As NotesStream
Dim vwSearchRequests As NotesView
Dim reqNum, totalNotify, totalAccepted, totalRejected, totalOOO, totalNoRes As Integer
Dim reqSer, reqJRSS, reqSPOC, reqNumStr As String
Dim reqDate As String
Dim reqNumColl As NotesDocumentCollection
Dim reqPanelRes As NotesItem
Dim reqPanelResValue As Variant

Set sess = New NotesSession
Set db = sess.CurrentDatabase
Set vwSearchRequests = db.GetView("RequestDocReport")
vwSearchRequests.Autoupdate = False
Set searchDoc = vwSearchRequests.GetFirstDocument

While Not searchDoc Is Nothing
    reqSer = "Service"
    reqJRSS = searchDoc.PS_JRSS(0)
    reqSPOC = "Hiring SPOC"
    totalAccepted = 0
    totalRejected = 0
    totalOOO = 0
    totalNoRes = 0
    totalNotify = 0

    reqNum = searchDoc.PS_RequestNo(0)
    reqNumStr = {PS_RequestNo = "} & reqNum & {"}
    Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(reqNumStr)
    Set reqNumDoc = reqNumColl.GetFirstDocument
    While Not reqNumColl Is Nothing

        If Not reqNumDoc.GetFirstItem("PanelResponse") Is Nothing Then
            reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse") 
            MsgBox CStr(reqPanelResValue(0))
            'Exit Sub
            If CStr(reqPanelResValue(0)) = "Accepted" Then
                totalAccepted = totalAccepted + 1
            End If
            If CStr(reqPanelResValue(0)) = "Rejected" Then
                totalRejected = totalRejected + 1
            End If
            If CStr(reqPanelResValue(0)) = "OOO" Then
                totalOOO = totalOOO + 1
            End If  
        Else
            If CStr(reqPanelResValue(0)) = "" Then
                totalNoRes = totalNoRes + 1
            End If
        End If

        totalNotify = totalNotify + 1
        Set reqNumDoc = reqNumColl.GetNextDocument(reqNumDoc)
    Wend

代码中的错误是什么?

后,代码卡住了
If Not reqNumDoc.GetFirstItem("PanelResponse") Is Nothing Then
                reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse")

2 个答案:

答案 0 :(得分:2)

而不是行

 While Not reqNumColl Is Nothing

 While Not reqNumDoc Is Nothing

你有一个不定式循环,因为即使你到达集合中的最后一个文档,集合reqNumColl也不是什么都没有。相反,您必须测试文档reqNumDoc

另一个问题可能是您的收集计算代码:

reqNumStr = {PS_RequestNo = "} & reqNum & {"}
Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(reqNumStr)

您对其进行编码的方式是视图中的第一个排序列应包含

PS_RequestNo = "12345"

可能您的视图在第一个排序列中只包含请求编号。如果是这样,您的代码就是:

Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(reqNum)

如果列包含数值或

Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(cStr(reqNum))

如果它包含字符串。

答案 1 :(得分:0)

除了您的代码中可能存在的任何其他问题(@Knut对于无限循环的原因是正确的),这不是一个好的模式:

If Not reqNumDoc.GetFirstItem("PanelResponse") Is Nothing Then
            reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse") 

如果您不需要,则会检索该项目两次。

这会好得多:

If reqNumDoc.HasItem"PanelResponse") Then
            reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse")