我对很陌生,我正在尝试从视图中获取数据并将其保存到字符串中。但是每次我这样做,我都会得到初始化对象变量在第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发布于 2015-02-25 15:13:40
第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发布于 2015-02-25 17:12:52
我会把它写成这样的。
我在您的代码中注意到:*您在错误处理程序中使用GoTo,应该是一份简历。*当代码到达时,您已经完成了"GoTo完成“,这是不需要的。*声明了几个不使用的变量。*你不用太多的错误检查,克努特的建议是个不错的建议。
这是我的建议,这就是我要导出的观点:
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),"~") & ";"这将在每个值之间加上一个~,如果有一个以上的值,那么您可以按照自己喜欢的方式处理。
https://stackoverflow.com/questions/28722325
复制相似问题