Hi All
A lot of people have posted messages in this forum about copying attachments from one doc to another.
The established method seems to be extracting the attached file(s) from the source doc (doc1) to a disk folder and then attaching the disk files to the target doc (doc2). Then you have to remove each attachment from disk to keep things clean.
This is a waste of resources and quite understandably most people don’t like it.
The code below will copy all the attachments from doc 1 to doc2 all in memory and without extracting to disk etc. This is intended form browser based documents.
Another major advantage of this method is that you don’t need to know the name of a richtextfield holding the attachment.
For example, if a document is accessed via a browser and attachments are created using a file upload control, then you can’t get hold of a richtextfield holding the attachments. And yet most examples of grabbing the attachments use something like:
Set rtitem = doc.GetFirstItem( “Body” )
which means you have to name/select the field that willbe checked for attachments. (By the way I tried doc.EmbeddedObjects but kept getting type mismatch)
With the method below you don’t need to know any field names. I have tested this method with documents in which the attachments are attached using a file upload control on the web and it works very well and works for multiple attachments too:
Call doc1.CopyAllItems(doc2)
Forall o In doc2.Items
If o.name <> "$FILE" Then
doc2.RemoveItem(o.name)
End If
End Forall
As you can see all items in doc1 are copied to doc2. Then in doc2 all the items that are not an attachment (“$FILE”) are removed. NOTE: if you check out the name of the item using something like:
Call agentLog.LogAction("item name is: " & o.name)
you will get “$file” in small letters which is very strange because only this capital letters work:
If o.name <> “$FILE” Then
Finally if your target document (doc2) already has items on it which you want to keep, simply create a new temporary document (doc3) that you don’t need to save. Use doc3 in instead of doc2 in the above example. You will then have doc3 which has all the attachments only. You can then do:
Call doc3.CopyAllItems(doc2, false)
which transfers only the attachments to doc2 (your target document).
You can also change things around and remove from (the in-memory) doc1 all items that are not attachments using the above Forall loop. Then copyallitems to doc2. BUT “don’t” save doc1 and you will still have all its original items, when your code stops.
I hope this helps.Good luck
Bizhan