Friday, May 15, 2009

Handling attachment with same name in Lotus Script

I have a problem of extracting attachments in a lotus notes document. If the document contains multiple files with same name, in the document it is displaying multiple documents with same name ; but while extracting the attachments one of the file is extracting properly and the other files with same name as extracting like 'ATT..' , 'ATW.." etc.

What happens is if a document contains multiple files with same name, domino internally will allocate a unique name to other files. Each file in notes document have two parts one is source and the other is name property. The source property will hold the atcual file name (that is u are seeing in document) , but the name property will hold a unique value supplied by domino for internal purposes.

Use a combination of name and source property to identify multiple files with same name and do some tricks while extracting.

Below is a small snippet I used while extracting...

Dim rtitem As NotesRichTextItem
Dim attname as String

Set rtitem = doc.GetFirstItem( "Body" )
While Not rtitem Is Nothing
Forall eo In rtitem.EmbeddedObjects
If (eo.Type = EMBED_ATTACHMENT) Then
' If Name and Source are different,
' Name is a random name assigned by Domino...

If eo.Name <> eo.Source Then
' take advantage of the random Name &
' add extension of the Source...

attname = eo.Name & "." & GetExtension(eo.Source)

' No random name was assigned,
' so it is safe to use Source...

attname = eo.Source

End If
Call eo.ExtractFile(attname)
End If
End Forall
' If multiple items in a document have the same name,
' programmatic access is limited to the first item.

' The remaining items yield invalid data.
' A work-around is to get the first item,
' process it, remove it,

' again get the first item (which was the second item),
' and so on until you process all the items
' with the same name.

' If you do not save the document,
' the items are not
' actually removed.

' However, the recommendation is that you avoid creating
' multiple items with the same name.

Call rtitem.Remove
Set rtitem = doc.GetFirstItem( "Body" )

No comments:

Post a Comment