LotusScript从视图中获取数据

时间:2015-02-25 15:01:08

标签: lotus-notes lotusscript

我是Lotus脚本的新手,我正在尝试从视图中获取数据并将其保存为字符串。但每次我这样做,我都会收到初始化对象变量未在第36行上设置的错误。在我的多米诺骨牌设计师Line 36正好在ItemNames(6)下。

我尝试使用我朋友的代码,我得到同样的错误,而他的作品没有问题。

请帮助我迫不及待地想要做这项工作。

Sub Initialize

    On Error GoTo ERRSUB

    Dim nSession As New NotesSession
    Dim nDb As NotesDatabase
    Dim nDoc As NotesDocument
    Dim view As NotesView
    Dim nitem As NotesItem

    Dim strRecord As String
    Dim DataString As String
    Dim nList List As String
    Dim ListCount As Integer
    Dim FirstLine As String 
    Dim counter As Integer
    counter = 0

    Dim ItemNames(6) As String  
    ItemNames(0) = "Date"
    ItemNames(1) = "Name"
    ItemNames(2) = "Name of buyer"
    ItemNames(3) = "Naziv of project"
    ItemNames(4) = "value"
    ItemNames(5) = "source"
    ItemNames(6) = "status" 

    Set nDb = nSession.Currentdatabase
    Set view = nDb.Getview("X_view_1")
    Set ndoc = view.Getfirstdocument()

    Do Until (ndoc Is nothing)

        ForAll item In ItemNames
            Set nitem = ndoc.Getfirstitem(item)
            DataString = nitem.Values & ";"
            counter = counter + 1
        End ForAll

        DataString = DataString & Chr(13)

        Set ndoc = view.Getnextdocument(ndoc)
    Loop    

    GoTo DONE

    DONE:   

    MessageBox counter
    Exit Sub    

    ERRSUB:

    Call logger("Error",nSession.currentagent.name,"Initialize","","")
    GoTo done       

End Sub

2 个答案:

答案 0 :(得分:3)

第36行是DataString = nitem.Values & ";"。错误是nitem未正确设置。可能该项目在某个文件中不可用。 nitem的测试不是Nothing

将ForAll循环更改为

    ForAll item In ItemNames
        Set nitem = ndoc.Getfirstitem(item)
        If Not nitem Is Nothing then
            DataString = DataString & nitem.Text
        End If 
        DataString = DataString & ";"
        counter = counter + 1
    End ForAll

答案 1 :(得分:0)

我会在下面写下这样的内容。

我在你的代码中注意到的事情: *您在错误处理程序中使用GoTo,应该是Resume。 *当代码到达那里时你有“GoTo DONE”,这是不需要的。 *您声明了几个不使用的变量。 *你没有使用太多的错误检查,Knut的建议很好。

以下是我的建议,这是我导出视图的方式:

Sub Initialize
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim view As NotesView
    Dim col As NotesViewEntryCollection
    Dim entry As NotesViewEntry
    Dim DataString As String
    Dim cnt List As Long

    On Error GoTo errHandler
    Set db = session.Currentdatabase
    Set view = db.Getview("X_view_1")
    Set col = view.AllEntries
    '*** Set counters
    cnt("total") = col.Count
    cnt("processed") = 0
    '*** Loop though all view entries, much faster that documents
    Set entry = col.GetFirstEntry()
    Do Until entry Is Nothing
        '*** Update status bar every 100 documents
        If cnt("processed") Mod 100 = 0 Then 
            Print "Processed " & cnt("processed") & " of " & cnt("total") & " documents."
        End If
        '*** Read view columns and add to string
        ForAll cv In entry.ColumnValues
            DataString = cv & ";"
        End ForAll
        '*** Add line break to string
        DataString = DataString & Chr(13)
        '*** Update counter and get next entry in view collection
        cnt("processed") = cnt("processed") + 1
        Set entry = col.GetNextEntry(entry)
    Loop    

exitSub: 
    MsgBox "Processed " & cnt("processed") & " of " & cnt("total") & " documents.",,"Finished" 
    Exit Sub    

errHandler:
    Call logger("Error",session.CurrentAgent.Name,"Initialize","","")
    Resume exitSub       
End Sub

另一种方法是直接从NotesDocument中读取值:

DataString = doc.GetItemValue(item)(0) & ";"

当然,这只会读取任何多值字段的第一个值,但您可以这样修复:

DataString = Join(doc.GetItemValue(item),"~") & ";"

如果有多个值,则会在每个值之间放置一个〜,然后您可以按自己喜欢的方式处理。