Examples: Working with attachments and embedded objects in LotusScript® classes

  1. This agent extracts the file attachments in the Body item of the current document using NotesRichTextNavigator methods to get the attachments.
    REM Run this against a selected document that has Body field
    REM Body field should contain file attachments
    Sub Initialize
      Dim session As NotesSession
      Dim db As NotesDatabase
      Dim dc As NotesDocumentCollection
      Dim doc As NotesDocument
      Dim body As NotesRichTextItem
      Dim rtnav As NotesRichTextNavigator
      Dim att As NotesEmbeddedObject
      Set session = New NotesSession
      Set db = session.CurrentDatabase
      Set dc = db.UnprocessedDocuments
      Set doc = dc.GetFirstDocument
      If Not doc.HasEmbedded Then Exit Sub
      Set body = doc.GetFirstItem("Body")
      Set rtnav = body.CreateNavigator
      
      REM Get attachments
      If rtnav.FindFirstElement(RTELEM_TYPE_FILEATTACHMENT) Then
        Do
          Set att = rtnav.GetElement()
          filepath$ = "C:\Files\" & att.Source
          Call att.ExtractFile(filepath$)
          Print filepath$ & " extracted"
        Loop While rtnav.FindNextElement()
      End If
    End Sub
  2. This agent extracts the file attachments in the Body item of the current document using NotesRichTextItem.EmbeddedObjects property to get the attachments.
    REM Run this against a selected document that has Body field
    REM Body field should contain file attachments
    Sub Initialize
      Dim session As NotesSession
      Dim db As NotesDatabase
      Dim dc As NotesDocumentCollection
      Dim doc As NotesDocument
      Dim body As NotesRichTextItem
      Set session = New NotesSession
      Set db = session.CurrentDatabase
      Set dc = db.UnprocessedDocuments
      Set doc = dc.GetFirstDocument
      If Not doc.HasEmbedded Then Exit Sub
      Set body = doc.GetFirstItem("Body")
      
      REM Get attachments
      Forall att In body.EmbeddedObjects
        If att.Type = EMBED_ATTACHMENT Then
          filepath$ = "C:\Files\" & att.Source
          Call att.ExtractFile(filepath$)
          Print filepath$ & " extracted"
        End If
      End Forall
    End Sub
  3. This form action example displays properties of all the embedded objects in a document.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Dim doc As NotesDocument
      Dim item As Variant
      Set uidoc = workspace.CurrentDocument
      Set doc = uidoc.Document
      Set item = doc.GetFirstItem("Body")
      Forall embobj In item.EmbeddedObjects
        verbs = "No verbs"
        Select Case embobj.Type
        Case EMBED_OBJECTLINK : _
        embobjType = "Object link"
        Case EMBED_ATTACHMENT : _
        embobjType = "Attachment"
        Case EMBED_OBJECT : embobjType = "Object"
          verbs = "Verbs:"
          Forall verb In embobj.Verbs
            verbs = verbs & " " & verb
          End Forall
        End Select
        Messagebox "Name: " & embobj.Name & Chr(10) _
        & "Class: " & embobj.Class & Chr(10) _
        & "File size: " & embobj.FileSize & Chr(10) _
        & "Type: " & embobjType & Chr(10) & verbs
      End Forall
    End Sub
  4. This form action example activates the first or only embedded object in a document.
    Sub Click(Source As Button)
      Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Dim doc As NotesDocument
      Dim item As Variant
      Set uidoc = workspace.CurrentDocument
      Set doc = uidoc.Document
      Set item = doc.GetFirstItem("Body")
      If Isempty (item.EmbeddedObjects) Then
        Messagebox "No embedded object in document"
        Exit Sub
      End If
      If item.EmbeddedObjects(0).Type <> EMBED_OBJECT Then
        Messagebox "Object not an embedded object"
        Exit Sub
      End If
      Call item.EmbeddedObjects(0).Activate(True)
    End Sub