I am extracting attachments in a document to user's local machine using LotusScript. The document has two rich text fields Body1
and Body2
and many a times only one of them has an attachment in it. I am using the below code snippet:
Dim doc As NotesDocument
Dim richTextItem As NotesRichTextItem
.....
.....
If doc.Hasembedded Then
Set richTextItem = doc.Getfirstitem("Body1")
ForAll o In richTextItem.Embeddedobjects
Call o.ExtractFile(dirName + "\" + o.Name)
End ForAll
Set richTextItem = doc.Getfirstitem("Body2")
ForAll o In richTextItem.Embeddedobjects
Call o.ExtractFile(dirName + "\" + o.Name)
End ForAll
End If
The problem is that if Body1
does not have attachment in it and Body2
does then the above code throws error of Type mismatch
on statement ForAll o In richTextItem.Embeddedobjects
and vice versa as there are no embedded objects in that rich text item. Also doc.Embeddedobjects
does not work because attachments are present inside rich text items. And the NotesRichTextItem
class does not have Hasembedded
property which can be used to check presence of attachments in it.
What would be a way out of this?
This is a short code if you want extract all attachments of a document no matter where they are stored:
Updated answer
Instead of accessing the
EmbeddedObjects
property directly in theforall
, assign it to avariant
and check it first, using theTypeName
function to make sure that the returned value was really an array ofNotesEmbeddedObject
objects, like this:Try this instead:
You may also want to extract the redundant logic into a subroutine.