2

I have the following code, which loops through the documents in TestView1, and for each document in that view loops through the documents in TestView2, performing some action (outputs to a file).

For the sake of keeping the loops relatively timely (there's heaps of older documents and I only want those time stamped from today or an upcoming date), I have found the first document for the current date, and stored its UNID so that that document can act as a starting position for the inner loop (the documents in MyView2 are in chronological order).

The inner loop runs correctly the first time, but come the second loop, I get the error message "The document is not in view TestView2". I have added what line breaks my code.

Here is my code:

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

Would appreciate any help.

Andre C
  • 457
  • 1
  • 9
  • 26
  • Do you save the document in inner loop so that's not listed in view anymore? If yes, get next document before you save current document. – Knut Herrmann Jul 15 '14 at 05:53
  • No, I don't do anything to the document itself. All I do is export a bunch of its fields to a file. Plus, it works entirely for the first outer loop, but as soon as the second iteration of the outer loop starts it breaks... I believe it has something to do with when I go back to the first document for the second round of iterations – Andre C Jul 15 '14 at 06:05
  • I believe so, because if it weren't, this would not have executed, would it? Set mDoc = mView.GetNextDocument(mDoc) – Andre C Jul 15 '14 at 06:13
  • But Set mDoc = mView.GetNextDocument(mDoc) actually executes successfully about 80 times, completing the inner loop. It's only on the second iteration of the outer loop (rDoc) that it fails – Andre C Jul 15 '14 at 06:18
  • rDoc belongs to rView.. Basically, rView has a bunch of rooms, with each rDoc being one room, and mView is a bunch of meetings with mDoc being one meeting. I'm looping through each room and extracting the meetings in that particular room, then I move onto the next room. It works fine for the first room (and works fine completely if I don't try to set the starting mDoc and just use GetFirstDocument), but as soon as I get to the second room, it fails – Andre C Jul 15 '14 at 06:23
  • Is room rDoc and meeting mDoc really the same document (= same UniqueId)? – Knut Herrmann Jul 15 '14 at 06:27
  • No, for that I compare room name in rDoc to a field that stores the room name in mDoc. And it only happens when I try to use a UNID to start browsing from today's meetings. If I leave that part out and go through all the meetings, it works... – Andre C Jul 15 '14 at 06:31
  • Oh, I see. You always start with the same document in mView. Hmm, should work this way... – Knut Herrmann Jul 15 '14 at 06:38
  • Is there a reason why you don't sort your mView by room/date+time and get first meeting with mView.GetDocumentByKey()? – Knut Herrmann Jul 15 '14 at 06:50
  • 1
    There was - it's a work DB, I don't have the authority to make such changes - but I managed to find a view in the database that was already organised, and your code worked great. Thanks – Andre C Jul 16 '14 at 01:31

2 Answers2

1

The thing is quite easy: When you get a document through its unid, you do NOT get it from the view. That way you will not get a "next" document. You need to "re- get" the document from view to be able to navigate.

Try this:

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

That should help.

Though mDoc stays the same document, this time it is initialized from view and can be used in "GetNextDocument"...

By the way: Using a NotesViewNavigator in my experience is most of the time faster to cycle through a view than to use the "native" NotesView- Methods.

EDIT: Same thing can occur, if you have a NotesDocument that is in a NotesDocumentCollection but not taken directly from there. Then you will get an error "document is not from this collection". There is is the same method: Set doc=collection.GetDocument( doc )

Tode
  • 11,795
  • 18
  • 34
0

There is a more efficient approach: categorize your mView by ResourceName + StartDate and use mView.getDocumentByKey(keys, true) to access the first relevant document in mView:

  1. Add a first categorized column ResourceName to mView
  2. Add a second categorized column StartDate to mView
  3. Change your code to:
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
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67