lotus-noteslotusscriptagentlotus-formuladomino-designer-eclipse

LotusScript - Multiple extraction and reattaching of attachments leads to copies of the first attachment and file corruption


Extracting and reattaching of single files produce the right results, but when it comes to multiple files the result is copies of the first attachment but with the names of the file name. For example, if I extract fileA.doc, fileB.doc and fileC.doc, I'll get fileA.doc, fileB.doc and fileC.doc but the content in the documents will be that of fileA.doc. File names are correct but the content is the content to the first document and some of the files becomes corrupt. Below is the same code.

strRtFieldName(0) ="Body1"
strRtFieldName(1) ="Body2"
strRtFieldName(2) ="Body3"
strRtFieldName(3) ="Body4"
strRtFieldName(4) ="Body5"
strRtFieldName(5) ="Body6"
strRtFieldName(6) ="Body7"
strRtFieldName(7) ="Body8"
strRtFieldName(8) ="Body9"
strRtFieldName(9) ="Body0"

varAttachmentNames = Evaluate("@AttachmentNames", doc) 

For i = 0 To UBound(varAttachmentNames)     
    ' check for attachment in Rich Text fields
    For k = 0 To 9
        Set rtitem = doc.Getfirstitem(strRtFieldName(k))
        If (object Is Nothing) Then
            If Not rtitem Is Nothing Then
                If (rtitem.Type = RICHTEXT) Then
                    Set object = rtitem.GetEmbeddedObject(varAttachmentNames(i))
                End If
            End If
        End If
    Next
    If (object Is Nothing) Then
        ' check for attachment in document
        Set object = doc.GetAttachment(varAttachmentNames(i))
    End If  
Next

Will appreciate if you can point out what's wrong with the code.


Solution

  • Review object variable, when it's set, impact on if statements. It should then be apparent that object is only nothing on the first loop. So subsequent loops are re-using the same attachment.

    I'm not sure why this is tagged wth XPages.