Call to getEmbeddedObjects(); (Domino Server API) returns wrong results

后端 未结 4 975
日久生厌
日久生厌 2021-01-19 04:15

The Domino server API getEmbeddedObjects(); returns the wrong result (zero) when a mail containing an attachment (as embedded object) is sent from the script.

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 04:23

    Lotus Notes does not provide a single reliable method for extracting attachments from a NotesDocument object, unfortunately. To be thorough, you'll need to check through all richtext items it contains, as well as the document object itself.

    I wrote the following code to extract attachments from selected emails in a mailbox, in an effort to cut down the file size (my users saved everything). The main loop is relevant to your question, though. It shows the process of looping through all of the document's items looking for richtext items with attachments, followed by a loop through all items again looking for items of type "Attachment".

    (forgive the hackiness of the code. It wasn't 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
    

提交回复
热议问题