Search code examples
lotus-noteslotusscript

Get first document returning nothing in Lotus


when I'm trying to access a field of the selected document in the current view, I get nothing.. My code:

Sub Click(Source As Button)
  Dim ws As New NotesUIWorkspace
  Dim uiview As NotesUIView 
  Dim dc As NotesDocumentCollection 
  Dim doc As NotesDocument 
  Dim receive As String 
  Set uiview=ws.CurrentView
  Set dc=uiview.Documents
  Set doc=dc.GetFirstDocument
  If doc Is Not Nothing Then
    receive=doc.GetItemValue("Field name")
  End If
End Sub

It never enters the if because doc is always nothing.. and when i remove the if, the error appears when getitemvalue tries to fetch something but it can't


Solution

  • dc.GetFirstDocument returns nothing because the collection is empty. That happens if you don't have select documents with a check mark. uiview.Documents delivers only those selected documents and not the highlighted document.

    If you only want to get the highlighted document in view (the document which is framed) enter image description here then this will work:

    Sub Click(Source As Button)
        Dim session As New NotesSession
        Dim doc As NotesDocument
        Set doc = session.DocumentContext
        If Not doc Is Nothing Then
            receive=doc.GetItemValue("FieldName")(0)
            Print receive
        End If
    End Sub
    

    session.DocumentContext returns the highlighted document in view.

    If you want to get all selected documents in view

    enter image description hereyou can use your slightly modified code

    Sub Click(Source As Button)
        Dim ws As New NotesUIWorkspace
        Dim uiview As NotesUIView
        Dim dc As NotesDocumentCollection
        Dim doc As NotesDocument
        Set uiview = ws.CurrentView
        Set dc = uiview.Documents
        Set doc = dc.GetFirstDocument
        While Not (doc Is Nothing)
            Print doc.GetItemValue ("FieldName")(0)
            Set doc = dc.GetNextDocument (doc)
        Wend
    End Sub