Examples: Accessing the current document

  1. This example executes when the user enters a field in Edit mode. The script displays information about the field and document.
    Sub Entering(Source As Field)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.CurrentDocument
      If uidoc.EditMode Then
        If uidoc.IsNewDoc Then
          Messagebox _
          ("This is the " & _
          uidoc.CurrentField & " field")
        Else
          Messagebox _
          ("This is the " & uidoc.CurrentField & _
          " field of " & uidoc.WindowTitle)
        End If
      End If
    End Sub
  2. This example puts the current document in Edit mode if it is not, and toggles the ruler.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.CurrentDocument
      If Not uidoc.EditMode Then
        uidoc.EditMode = True
      End If
      uidoc.Ruler = NOT uidoc.Ruler
    End Sub
  3. This example gets the database document that corresponds to the current UI document, then gets the parent database (that is, the current database).
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Dim doc As NotesDocument
      Dim db As NotesDatabase
      Set uidoc = workspace.CurrentDocument
      Set doc = uidoc.Document
      Set db = doc.ParentDatabase
      Messagebox "Parent database: " & db.Title
    End Sub
  4. This example gets the selected text in the current document.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Dim text As String
      Set uidoc = workspace.CurrentDocument   
      text = uidoc.GetSelectedText(body$)
    End Sub
  5. This script first displays the names of users who have modified the document, then opens the document.
    Sub Queryopendocument(Source As Notesuiview, Continue As Variant)
      Dim docs As NotesDocumentCollection
      Dim doc As NotesDocument
      Dim alist As String
      Set docs = Source.Documents
      Set doc = docs.GetFirstDocument()
      Forall a In doc.Authors
        alist = alist & a & Chr(10)
      End Forall
      Messagebox alist  , , "Document author(s)"
    End Sub
  6. This example places a value in the Categories text item using a NotesDocument object. When the NotesDocument object is saved, the workspace object is updated in real time. This code won't work for a rich text item.
    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
      If Not uidoc.EditMode Then uidoc.EditMode = True
      categories = doc.Categories
      categories(0) = "Main category"
      doc.Categories = categories
      Call doc.Save(True, False)
    End Sub
  7. This example composes a new document based on the "Person" form, and displays a dialog box based on the "Person Dialog Box" form to get information from the user. The "Person Dialog Box" form contains a layout region for which the fields have names that correspond to some of the fields in the "Person" form. When the user clicks OK in the dialog box, the data in the dialog box transfers to the fields of the same name in the new document.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.ComposeDocument("","", "Person")
      Call workspace.DialogBox _
      ("Person Dialog Box", True, True)
    End Sub
  8. This example opens the current document in a view in Edit mode and displays a dialog box based on the "Person Dialog Box" form to get information from the user. The "Person Dialog Box" form contains a layout region for which the fields have names that correspond to some of the fields in the current document. When the user clicks OK in the dialog box, the data in the dialog box transfers to the fields of the same name in the document.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.EditDocument(True)
      Call workspace.DialogBox _
      ("Person Dialog Box", True, True)
    End Sub
  9. This example is the same as the last but uses all the arguments to the DialogBox method. Argument 4 (True) displays only an OK button, argument 5 (True) refuses to add new fields to the underlying form, argument 6 (False) allows edits to be passed to the underlying form, argument 7 (False) allows the dialog box to be edited, and the last argument specifies a title for the dialog box.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.EditDocument(True)
      Call workspace.DialogBox _
      ("Person Dialog Box", _
      True, True, True, True, False, False, _
      "Fill in this box and click Enter")
    End Sub
  10. This example finds a specified string in the current UI document.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.CurrentDocument
      Call uidoc.FindString("Get this string.")
    End Sub
  11. This example imports a text document into the body of the current UI document.
    Sub Click(Source As Button)  
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.CurrentDocument
      Call uidoc.Import
    End Sub
  12. This example puts the current document in Edit mode and checks it for spelling errors. The document must be in Edit mode before the SpellCheck method is called.
    Sub Click(Source As Button)  
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.EditDocument(True)
      Call uidoc.SpellCheck
    End Sub
  13. This example brings up the Find Free Time box and searches for available meeting times for the names listed in the SendTo field. The search is restricted to the range defined in the StartDateTime and EndDateTime fields.
    Sub Click(Source As Button)  
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Set uidoc = workspace.CurrentDocument
      Call uidoc.Refresh
      Call uidoc.FindFreeTimeDialogEx _
      ("SendTo", "", "", "", "", "", "", "StartDateTime", _
      "EndDateTime")
    End Sub