LotusScript 从视图中获取数据
LotusScript getting data from a view
我是 Lotus 脚本的新手,我正在尝试从视图中获取数据并将其保存到字符串中。但每次我这样做时,我都会收到 Initialize Object variable not set on line 36 的错误。在我的多米诺设计器中,第 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
第 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
我会在下面这样写。
我在您的代码中注意到:
* 您在错误处理程序中使用 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),"~") & ";"
如果有多个值,这将在每个值之间加上一个 ~,然后您可以按照自己喜欢的方式处理它。
我是 Lotus 脚本的新手,我正在尝试从视图中获取数据并将其保存到字符串中。但每次我这样做时,我都会收到 Initialize Object variable not set on line 36 的错误。在我的多米诺设计器中,第 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
第 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
我会在下面这样写。
我在您的代码中注意到: * 您在错误处理程序中使用 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),"~") & ";"
如果有多个值,这将在每个值之间加上一个 ~,然后您可以按照自己喜欢的方式处理它。