1
votes

I'm working on a lotus script code that extracts attachments from documents. The code works fine except for a few documents and throws an error: Error Code:91 - Error: Object variable not set

Below is the code I'm using to do the extraction:

   If (doc.HasItem("$File")) Then
       attachments = Evaluate("@AttachmentNames", doc)                          
       Forall o In attachments      
            Set AttachmentName = doc.GetAttachment(o)
            Call AttachmentName.ExtractFile(Attachfoldername & "\" & AttachFileNames)                                   
       End Forall
   End If

On debugging I found that the AttachmentName variant is not getting set and just shows [NOTESEMBEDDEDOBJECT] value for it once I step into the next line. Please see screen print below: AttachmentName variable not set

Also you can see from the above image that the attachment name contains certain junk characters. There were few other documents for which the code failed and returned the same error. All of these had attachments with file names having strange characters (Ex.: "■ß■■1.jpg", "AR Médipel faible teneur en capasaïcine.doc").

There are no rich text fields in the form that is used and I do not have access to see the design of the form but I can see the $FILE field in the document properties. I initially thought that problem could be with the file name, so for testing this I attached a file with the same name into a rich text field of a test document. This time the code worked successfully extracting the attachment as expected. So now I'm left wondering what might be the actual problem.

I'm a beginner with Lotus Script so I don't know if I'm missing something.

Any help to resolve this would be greatly appreciated. Thanks in advance!

2

2 Answers

3
votes

I would try using doc.EmbeddedOjbects() instead of Evaluate("@AttachmentNames"). That returns an array of NotesEmbdeddedObject, so you would not need to use doc.getAttachment().

I know that using Evaluate("@AttachmentNames") is a useful technique when you have a combination of attachments that are in rich text fields and attachments that are not, but you've said there are no rich text fields so I think using method should find your attachments, and if there's something funny going on with the attachment names on the boundary between formula and LotusScript, using doc.EmbeddedObjects may avoid that problem.

Your code would look something like this (untested!):

    If (doc.HasItem("$File")) Then
       attachments = doc.EmbeddedObjects()                          
       Forall o In attachments   
            if o.Type() = EMBED_ATTACHMENT then
                Call o.ExtractFile(Attachfoldername & "\" & o.Name())
            endif                                   
       End Forall
   End If
0
votes

You are VERY close to what you need. The EmbeddedObjects array for a NotesDocument does NOT include Attachments - so you will need the list of Attachment File Names to use in the GetAttachment method. I declared the attachments variable as a variant - so I force the String value in the GetAttachment function call to avoid type mismatch issues.

    attachments = Evaluate("@AttachmentNames", Doc)     
     For x = 0 To UBound(attachments)
        Set embedObj = Doc.GetAttachment(CStr(attachments(x)))
        filepath = tempDir + "\" + CStr(attachments(x))
        Call embedObj.Extractfile(filepath)
    Next