2

我有以下代码,它循环遍历 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

将不胜感激任何帮助。

4

2 回答 2

1

事情很简单:当您通过其 unid 获取文档时,您不会从视图中获取它。这样你就不会得到“下一个”文件。您需要从视图中“重新获取”文档才能导航。

尝试这个:

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 保持同一个文档,但这次它是从视图中初始化的,可以在“GetNextDocument”中使用......

顺便说一句:根据我的经验,使用 NotesViewNavigator 在大多数情况下循环浏览视图比使用“本机” NotesView- 方法更快。

编辑:如果您有一个位于 NotesDocumentCollection 中但不是直接从那里获取的 NotesDocument,则可能会发生同样的事情。然后你会得到一个错误“文档不是来自这个集合”。有同样的方法:Set doc=collection.GetDocument( doc )

于 2014-07-15T06:49:15.307 回答
0

有一种更有效的方法:通过 ResourceName + StartDate 对您的 mView 进行分类,并用于mView.getDocumentByKey(keys, true)访问 mView 中的第一个相关文档:

  1. 将第一个分类列添加ResourceName到 mView
  2. StartDate向 mView添加第二个分类列
  3. 将您的代码更改为:
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
于 2014-07-15T18:17:31.947 回答