developerWorks  >  Lotus  >  Forums & community  >  Notes/Domino 6 and 7 Forum

Notes/Domino 6 and 7 Forum

developerWorks




Extracting attachments - doc.GetFirstItem("Body") returns Nothing
Keith Walker 09/25/2013 12:25 PM
Applications Development 7.0.2 Windows 2003


I’m trying to export all of the documents and their attachments from a Lotus Notes database (with Designer 7.0). I can get the document data and can get an attachment, but only if I hard code the name. The two methods, in LotusScript, I’ve found for getting the filename programmatically aren’t working, as shown in the lower two code blocks. In the first, doc.GetFirstItem( "Body" ) returns Nothing, and in the second, there’s a Type Mismatch during execution on the Forall line.
Any help on how to extract the attachments would be greatly appreciated! I’m not sure whether the attachments are stored as “attachments” or OLE, but I suspect as attachments, since they’re primarily PDFs.
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim query As String
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Dim fileCount As Integer
Dim attachment As NotesEmbeddedObject
Dim fileName As String

Set db = session.CurrentDatabase
' get a document that has an attachment
Set collection = db.FTSearch( "06/25/2013", 10 )

fileNum% = Freefile()
fileName$ = "c:\kcw\lotusexport.txt"
Open fileName$ For Output As fileNum%
Write #fileNum%, "docs found", collection.Count

Set doc = collection.GetFirstDocument
' write out document properties
Forall x In doc.Items
Write #fileNum%, x.Name, " = ", x.Text
End Forall
'extract document (using hardcoded name)
Set attachment = doc.GetAttachment("OCSE-FRONT_SCANTODESKTOP_06262013-104822.pdf")
Call attachment.ExtractFile _
( "c:\kcw\attachment" )

'try to get attachment through "Body", but rtitem is Nothing
Set rtitem = doc.GetFirstItem( "Body" )
Write #fileNum%, "rtitem is Nothing", rtitem Is Nothing
fileCount = 0
If Not rtitem Is Nothing Then
If ( rtitem.Type = RICHTEXT ) Then
Write #fileNum%, "rtitem is RICHTEXT"
Forall o In rtitem.EmbeddedObjects
Write #fileNum%, "has Embedded Objects"
fileCount = fileCount + 1
Write #fileNum%,"rtitem num", fileCount
Call o.ExtractFile _
( "c:\kcw\newfile" & Cstr(fileCount) )
End Forall
End If
End If

'Fails with "Type mismatch" at Forall loop
If doc.HasEmbedded Then
Write #fileNum%, "doc has embedded"
Forall objects In doc.EmbeddedObjects
Write #fileNum%, "in for loop"
Write #fileNum%, "filename= ", object.Source
End Forall
End If

Close fileNum%
End Sub

Go back