Lotus Notes does not provide a single reliable method for extracting attachments from a NotesDocument object, unfortunately. To be thorough, you need to check all the richtext elements contained in it, as well as the document object itself.
I wrote the following code to extract attachments from selected emails in a mailbox to reduce file size (my users saved everything). However, the main loop is relevant to your question. It shows the process of iterating over all elements of a document that are looking for richtext elements with attachments, and then loop through all the elements again looking for elements of type "Attachment".
(forgive hacking code. It was not written for efficiency)
Sub Initialize Set s = New NotesSession Set db = s.CurrentDatabase Set dc = db.UnprocessedDocuments Set doc = dc.GetFirstDocument Dim rtItem As NotesRichTextItem Dim RichTextItemNames List As String Dim DocumentItemNames List As String Dim itemCount as Integer While Not (doc Is Nothing) 'Scan all richtext items in document for embedded objects Forall i In doc.Items If i.Type = RICHTEXT Then Set rtItem = doc.GetFirstItem(i.Name) If Not Isempty(rtItem.EmbeddedObjects) Then RichTextItemNames(itemCount) = Cstr(i.Name) itemCount = itemCount + 1 End If End If End Forall 'Loop through richtext items and extract the embedded attachments For j = 0 To itemCount - 1 Set rtItem = doc.GetfirstItem(RichTextItemNames(j)) Forall Obj In rtItem.EmbeddedObjects If ( Obj.Type = EMBED_ATTACHMENT ) Then Call ExportAttachment(Obj) Call Obj.Remove Call doc.Save( False, True ) 'creates conflict doc if conflict exists End If End Forall Next 'Scan all items in document for Attachment type items itemCount = 0 Forall i In doc.Items If i.Type = ATTACHMENT Then DocumentItemNames(itemCount) = i.Values(0) itemCount = itemCount + 1 End If End Forall 'Loop through all attachment items in document and extract them For j = 0 To itemCount - 1 Set attachmentObject = doc.GetAttachment(DocumentItemNames(j)) Call ExportAttachment(attachmentObject) Call attachmentObject.Remove Call doc.Save( False, True ) 'creates conflict doc if conflict exists Next Set doc = dc.GetNextDocument(doc) Wend End Sub Sub ExportAttachment(o As Variant) Dim sAttachmentName As String Dim sNum As String Dim sTemp As String ' Append number to end of filename if filename exists. sAttachmentName = sDir & "\" & o.Source While Not (Dir$(sAttachmentName, 0) = "") sNum = Right(Strleftback(sAttachmentName, "."), 2) If Isnumeric(sNum) Then sTemp = Strleftback(sAttachmentName, ".") sTemp = Left(sTemp, Len(sTemp) - 2) sAttachmentName = sTemp & Format$(Cint(sNum) + 1, "##00") & _ "." & Strrightback(sAttachmentName, ".") Else sAttachmentName = Strleftback(sAttachmentName, ".") & _ "01." & Strrightback(sAttachmentName, ".") End If Wend Print "Exporting " & sAttachmentName 'Save the file Call o.ExtractFile( sAttachmentName ) End Sub
source share