我有以下代码,它循环遍历TestView1中的文档,并且对于该视图中的每个文档循环遍历TestView2中的文档,执行一些操作(输出到文件)。
为了保持循环相对及时(有大量旧文档,我只想要从今天或即将到来的日期加盖时间),我找到了当前日期的第一个文档,并存储了它的UNID,以便该文档可以作为内部循环的起始位置(MyView2中的文档按时间顺序排列)。
内循环第一次正确运行,但是第二次循环,我收到错误消息“文档不在视图TestView2中”。我添加了一行代码。
这是我的代码:
Dim sess As New NotesSession
Dim db As NotesDatabase
Dim rDoc As NotesDocument
Dim mDoc As NotesDocument
Dim rView As NotesView
Dim mView As NotesView
Dim unid As String
Dim todayDateTime As New NotesDateTime("Today")
Set db = sess.currentDatabase
Set rView = db.GetView("TestView1")
Set rDoc = rView.GetFirstDocument
Set mView = db.GetView("TestView2")
Set mDoc = mView.GetFirstDocument
Set mDoc = mView.GetFirstDocumentb 'Finds the UNID of the first document of the day
Do While Not (mDoc Is Nothing)
Dim startDate As New NotesDateTime(mDoc.startDate(0))
If startDate.DateOnly = todayDateTime.DateOnly Then
unid$ = mDoc.UniversalID
Exit Do
End If
Set mDoc = mView.GetNextDocument(mDoc)
Loop
While Not (rDoc Is Nothing) 'Outer Loop
If rDoc.Site(0) = "SAMPLE" Then
<CODE>
If Not unid$ = "" Then
Set mDoc = db.Getdocumentbyunid(unid$)
While Not (mDoc Is Nothing) 'Inner Loop
If mDoc.ResourceName(0) = rName$ Then
<PRINT TO FILE CODE>
End If
Set mDoc = mView.GetNextDocument(mDoc) <--- This line here breaks the code**
Wend
End If
End If
Set rDoc = rView.GetNextDocument(rDoc)
Wend
感谢任何帮助。
答案 0 :(得分:1)
事情很简单:当你通过unid获取文档时,你不会从视图中获取它。这样你就不会得到下一个&#34;文献。你需要&#34;得到&#34;视图中的文档能够导航。
试试这个:
Dim viewNav as NotesViewNavigator
Set viewNav = mView.CreateViewNav
...
Set mDoc = db.Getdocumentbyunid(unid$)
'- now get the same document, but this time from view:
Set mDoc = viewNav.getEntry( mDoc ).Document
这应该有所帮助。
虽然mDoc保持相同的文档,但这次它是从视图初始化的,可用于&#34; GetNextDocument&#34; ...
顺便说一句:根据我的经验,使用NotesViewNavigator大部分时间在视图中循环比使用&#34; native&#34;更快。 NotesView-方法。
编辑:如果你有一个NotesDocumentCollection中的NotesDocument但是没有直接从那里获取,那么同样的事情可能发生。然后你会得到一个错误&#34;文件不是来自这个集合&#34;。方法相同:Set doc=collection.GetDocument( doc )
答案 1 :(得分:0)
有一种更有效的方法:按资源名称+ StartDate对mView进行分类,并使用mView.getDocumentByKey(keys, true)
访问mView中的第一个相关文档:
ResourceName
添加到mView StartDate
添加到mView Dim sess As New NotesSession
Dim db As NotesDatabase
Dim rDoc As NotesDocument
Dim mDoc As NotesDocument
Dim rView As NotesView
Dim mView As NotesView
Dim rName As String
Dim keys(1) As Variant
Set db = sess.currentDatabase
Set rView = db.GetView("TestView1")
Set rDoc = rView.GetFirstDocument
Set mView = db.GetView("TestView2")
While Not (rDoc Is Nothing)
If rDoc.Site(0) = "SAMPLE" Then
rName = rDoc.ResourceName(0)
' <CODE>
keys(0) = rName
keys(1) = Today
Set mDoc = mView.GetdocumentbyKey(keys, true)
While Not (mDoc Is Nothing)
If mDoc.ResourceName(0) = rName Then
' <Print To FILE CODE>
Set mDoc = mView.GetNextDocument(mDoc)
Else
Set mDoc = Nothing
End if
Wend
End If
Set rDoc = rView.GetNextDocument(rDoc)
Wend