Manage Learn to apply best practices and optimize your operations.

Copy attachments in LotusScript without detaching

This procedure copies file attachments from the current Web document to an existing document.

View member feedback to this tip.

This procedure copies file attachments from the current Web document to an existing document (but it can be modified to work for the Notes client as well just by changing the docSource object).

There is no direct way to copy file attachments -- copying the $FILE item with CopyItemToDocument and using doc.ReplaceItemValue do not work on $FILE items. Using doc.CopyAllItems works, but that is not desirable, as I only want to copy the attachments.

The normal workaround is to detach attachments from the source document to the hard drive and re-attach them to a rich text field on the target document. However, that's often restricted on a server, so I'd like to avoid it.

Here is a better strategy:

  1. Call the function below, passing in the database handle along with the source document and target document objects.

  2. Create new docTemp as blank document.

  3. Copy all items from docSource to docTemp.

  4. Loop through docTemp and delete all items not named $File. Now you have a docSource that only has the attachment(s) on it!

  5. RemoveItem($File) from docTarget (to clear existing attachments).

  6. Call docTemp CopyAllItems to docTarget (this should only copy the attachment).

  7. Save docTarget.

Sub CopyAttachments 
(db As NotesDatabase, docSource As 
NotesDocument, docTarget As 
NotesDocument)
 Dim docTemp As NotesDocument 
 
 'Remove all attachments from existing document
 Call docTarget.RemoveItem ("$FILE")
 
 'Create a temporary document and 
use CopyAllItems from the current 
document
 'because I know that will include the attachment
 Set docTemp = db.CreateDocument
 Call docSource.CopyAllItems (docTemp)
 
 'Clear all items not named $File 
from the temp docSource, so that all 
that's left is the attachment
 Forall item In docTemp.Items
  If Not (Ucase(item.Name) = "$FILE") Then
   docTemp.RemoveItem (item.Name)
  End If  
 End Forall
 
 'Now, docTemp only has the attachments left on it
 'CopyAllItems will copy the attachment, 
so use it to copy to docTarget
 Call docTemp.CopyAllItems (docTarget)
 Call docTarget.Save (True, False)
End Sub

MEMBER FEEDBACK TO THIS TIP

This is a very easy way to do a very useful thing.

—Alejandro M.

******************************************

This is a great tip. However, I was trying to e-mail attachments to another person using a new document I created in LotusScript, but the attachments did not send.

—Annonymous

******************************************

I'm pretty sure you would need to save the document for the attachments to be stored on it. This isn't necessary with most types of fields, but it is usually the case with rich text data. (You just don't normally save documents that are created in the background only to be e-mailed out.)

—Brad Balassaitis, tip author

******************************************

Thanks for the tip. I'm using R5, and have no chance of testing whether this is required for other versions, but in my case I also had to copy the rich text field in which the file was originally attached to have it work properly. Without it the docTemp that contained only the $FILE field lost its Items-property entirely.

—Jukka D.

******************************************

When I created this code, I tested it in a web application, where attachments are not tied to specific rich text fields. This user probably used it in a Notes client application where the attachment was stored in a specific field, so this makes sense.

—Brad Balassaitis, tip author

Do you have comments on this tip? Let us know.

Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close