Examples: Locating documents within a view or folder

  1. This example accesses all the documents in the "By Category" view first to last. Alternatively, use the GetLastDocument and GetPrevDocument methods to access all the documents last to first.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim view As NotesView
      Dim doc As NotesDocument
      Set db = session.CurrentDatabase
      Set view = db.GetView("By Category")
      Messagebox "View name: " & view.Name
      Set doc = view.GetFirstDocument
      While Not(doc Is Nothing)
        Messagebox "Subject: " & doc.Subject(0)
        Set doc = view.GetNextDocument(doc)
      Wend
    End Sub
  2. This example limits the view to those documents that meet the full-text search criteria for "Search string alpha" and traverses the view.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim view As NotesView
      Dim doc As NotesDocument
      Set db = session.CurrentDatabase
      Set view = db.GetView("By Category")
      Call db.UpdateFTIndex(True)
      numDocs = view.FTSearch("""Search string alpha""", 0)
      If numDocs <> 0 Then
        Set doc = view.GetFirstDocument
        While Not(doc Is Nothing)
          Messagebox "Subject: " & doc.Subject(0)
          Set doc = view.GetNextDocument(doc)
        Wend
      End If
    End Sub
  3. This example uses GetAllDocumentsByKey to access all the documents in the "By Subject" view with whatever the user enters in response to Inputbox as the first characters of the first sorted column.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim view As NotesView
      Dim dc As NotesDocumentCollection
      Dim keyString As String
      keyString = Inputbox("Subject starts with?")
      Set db = session.CurrentDatabase
      Set view = db.GetView("By Category")
      Set dc = view.GetAllDocumentsByKey(keyString, False)
      Set doc = dc.GetFirstDocument()
      If dc Is Nothing Then
         Messagebox keyString,, "Not found"
         Exit Sub
      End If
      While Not(doc Is Nothing)
         Messagebox doc.Subject(0)
         Set doc = dc.GetNextDocument(doc)
      Wend
    End Sub
  4. This example uses GetDocumentByKey and GetNextDocument to access all the documents in the "By Subject" view with whatever the user enters in response to Inputbox as the first characters of the first sorted column.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim view As NotesView
      Dim doc As NotesDocument
      Dim keyArray(1 To 1) As String
      keyArray(1) = Inputbox("Subject starts with?")
      Set db = session.CurrentDatabase
      Set view = db.GetView("By Subject")
      Set doc = view.GetDocumentByKey(keyArray)
      If doc Is Nothing Then
        Messagebox keyArray(1) & " not found"
        Exit Sub
      End If
      subj = Left(doc.Subject(0), Len(keyArray(1)))
      While Not(doc Is Nothing) And subj = keyArray(1)
        Messagebox "Subject: " & doc.Subject(0)
        Set doc = view.GetNextDocument(doc)
        subj = Left(doc.Subject(0), Len(keyArray(1)))
      Wend
    End Sub
  5. This example accesses all the documents in a view by moving up and down through all response and response to response documents.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim view As NotesView
      Dim doc As NotesDocument
      Dim Child As NotesDocument
      Dim grandChild As NotesDocument
      Set db = session.CurrentDatabase
      Set view = db.GetView("By Subject")
      Set doc = view.GetFirstDocument
      While Not(doc Is Nothing)
        Messagebox "Main document: " & doc.Subject(0)
        Set child = view.GetChild(doc)
        While Not(child Is Nothing)
          Messagebox "Response: " & child.Subject(0)
          Set grandChild = view.GetChild(child)
          While Not(grandChild Is Nothing)
            Messagebox "Response to response: " & _
            grandChild.Subject(0)
            Set grandchild = _
            view.GetNextSibling(grandChild)
          Wend
          Set child = view.GetNextSibling(child)
        Wend
        Set doc = view.GetNextSibling(doc)
      Wend
    End Sub
  6. This example displays, for each document in the "By Category" view, a message saying whether or not the document is a response document, and giving the count for its first-level response documents.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim view As NotesView
      Dim dc As NotesDocumentCollection
      Dim doc As NotesDocument
      Set db = session.CurrentDatabase
      Set view = db.GetView("By Category")
      Set doc = view.GetFirstDocument
      While Not(doc Is Nothing)
        Set dc = doc.Responses
        If doc.IsResponse Then
          isResponse = _
          "This document is a response document."
        Else
          isResponse = _
          "This document is not a response document."
        End If
        Messagebox "Subject: " & doc.Subject(0) _
        & Chr(10) & isResponse & Chr(10) & _
        "This document has " & dc.Count _
        & " response documents."
        Set doc = view.GetNextDocument(doc)
      Wend
    End Sub
  7. This example, which is a button on a form, puts a copy of the current document in the folder named "My Favorite Documents."
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Dim doc As NotesDocument
      Set uidoc = workspace.CurrentDocument
      Set doc = uidoc.Document
      Call doc.PutInFolder("My Favorite Documents")
    End Sub