Lotusscript, sending attachments from one database to another

Posted on 2006-07-21
Last Modified: 2013-12-18
Hi Guys,

First off, although I have a good understanding of Notes I am not a Notes developer.

We have a script that we use to send documents from one database for use in another when required.

This scripts when run on one more documents does the job just fine, ensuring that existing documents are just updated and not recreated.

Now we need also to send over attachments, documents may have none, one or more attachments.

Existing working script below, any help would be appreciated, thks

Sub Initialize
      Dim ss As New notessession
      Dim sourcedb As notesdatabase
      Dim col As notesdocumentcollection
      Dim sourcedoc As notesdocument
      Dim sourcertitem As notesrichtextitem
      Dim destdb As notesdatabase
      Dim destview As notesview
      Dim destdoc As notesdocument
      Dim n As Variant
      Dim unid As String
      Set sourcedb = ss.currentdatabase
      Set destdb = New notesdatabase("server/domain", "folder\db.nsf")
      Set destview = destdb.getview("DB")
      Set col = sourcedb.unprocesseddocuments
      Set sourcedoc = col.getfirstdocument()
      While Not(sourcedoc Is Nothing)
            unid = sourcedoc.DocID(0)
            Set destdoc = destview.getdocumentbykey(unid)
            If destdoc Is Nothing Then
                  Set destdoc = New notesdocument(destdb)
            End If
            Set sourcertitem = sourcedoc.getfirstitem("Body")
            If destdoc.hasitem("body") Then
                  Call destdoc.removeitem("body")
            End If
            Call sourcertitem.copyitemtodocument(destdoc, "Body")
            If destdoc.hasitem("$Fonts") Then
                  Call destdoc.removeitem("$Fonts")
            End If
            Set n = sourcedoc.getfirstitem("$Fonts")
            Call n.copyitemtodocument(destdoc,"")
            Dim articleItem As notesitem
            Set articleItem = sourcedoc.getfirstitem( "body" )
            Call destdoc.replaceitemvalue("pricingRules", "")
            Call destdoc.replaceitemvalue("display1", sourcedoc.Title)
            Call destdoc.replaceitemvalue("display2", "")
            Call destdoc.replaceitemvalue("Form", "Report")
            Call, False)
            Set sourcedoc = col.getnextdocument(sourcedoc)
End Sub
Question by:tashsmith
  • 7
  • 3
  • 2
  • +1
LVL 46

Expert Comment

by:Sjef Bosman
ID: 17153104
What's your problem? Attachments aren't transferred at the moment? Apparently, the attachments are not in the Body-field but in some other field, or in the document itself? Do you know where thery are stored?

Author Comment

ID: 17153120
Attachments are not being transferred, they are attached to the document in an rtf field. We now need them to transfer as well.
LVL 19

Accepted Solution

madheeswar earned 100 total points
ID: 17153128
u should be using Copyallitems to copy those attachments.
Else, u need to Extract the attachment and then attach to the new doc in the other db.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


Author Comment

ID: 17153140
if I use Copyallitems presumably I will transfer all the document including all the fields, which we do not want to do.
Is this the case?
LVL 18

Assisted Solution

marilyng earned 300 total points
ID: 17153145
Wouldn't just duplicating this section for the rtItem that holds the attachments do it?

Set sourcertitem = sourcedoc.getfirstitem("rtItemwAttachments")
If destdoc.hasitem("rtItemwAttachments") Then
               Call destdoc.removeitem("rtItemwAttachments")
          End If
Call sourcertitem.copyitemtodocument(destdoc, "rtItemwAttachments")
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 100 total points
ID: 17153340
Indeed, the same you already did for the Body field??

Author Comment

ID: 17153396
ok guys thanks, gonna give that a go...

Author Comment

ID: 17154588
ok right well,

I included this code into the original code and the attachment/s tranferred to the new document.
However on opening the destination document I get the following error 'note item not found'.

When the document is in edit mode the "attach" field does display a empty icon and the file launches successfully.
I then saved the documents and looked at the field values in document properties.

The "attach" field shows no value.
The "ListAttachNames" that uses formulae @AttachmentNames, as lists the attachments name just fine.
The $File field shows the file details

However when I open the document again, the attach field appears empty, not even a empty icon box, all field values remain as above, but it is not possible to delete from the 'attach' rtf field as nothing is seen either in the properties list or in the form itself.

Ive done some digging and it seems this is a bug in Designer versions lower than 6.5.5 Im running 6.5.4... happy days

This problem is documented  here with various options for a fix.

The only resonable option for me is the workaround suggesting the ExtractFile method, pasted below...
Although I would want to script to delete the files stored on the hardrive after reattaching, I assume the Kill line does this?


To fix the original source file attachment, save it to disk, delete it from the Notes document, save the Notes document, and then reattach the file.

To code around the issue in earlier releases, do not use a CopyItem method. Instead use the ExtractFile method (of the NotesEmbeddedObjects class) to write the attachment to disk. Then use the EmbedObject method (of the NotesRichTextItem class) to attach the file.

For example:

'Unremark the line below copying to a new document
'Dim RTtarget as new NotesRichTextItem(targetdoc, "Body")
Set RTtarget=targetdoc.GetFirstItem("Body")
Set RTsource=sourcedoc.GetFirstItem("Body")
Forall o In RTsource.embeddedobjects
Call o.extractfile("C:\" + o.source)
Call RTtarget.embedobject(Embed_Attachment, "", "C:\" + o.source)
Kill "C:\" + o.source
End If
End Forall


Author Comment

ID: 17155152
I ran the script using Notes 7 and am still experiencing the same problems, have you guys come across this problem?
LVL 18

Expert Comment

ID: 17155357
Probably not, because I would just copy the entire document over and then delete the fields I don't need and change the form name.   However, since Item retains all properties of RT item, then try:

targetdoc.ItemName = source.docItem


Author Comment

ID: 17155401
OK I got this to work, at the moment it seems to do the job, but Im going to test some more...

Dim RTtarget As New NotesRichTextItem(destdoc, "attach")
            Set RTtarget=destdoc.GetFirstItem("attach")
            Set RTsource=sourcedoc.GetFirstItem("Additional")
            Forall o In RTsource.embeddedobjects
                  If o.type=EMBED_ATTACHMENT Then
                        Call o.extractfile("C:\Desktop" + o.source)
                        Call RTtarget.embedobject(Embed_Attachment, "", "C:\Desktop" + o.source)
                        Kill "C:\Desktop" + o.source
                  End If
            End Forall
LVL 18

Expert Comment

ID: 17158259

If this is running on the server, then I'm thinking that the source directory on the server would be the Notes\data directory, else the detach might not function to a directory outside of notes.

But I could be wrong.. good luck.

Author Comment

ID: 17179467
thanks guys, got there in the end, you all helped to the final solution so awarded accordingly...

marilyng the attachments are detaching to my hardrive just fine...


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lack of Storage capacity is a common problem that exists in every field of life. Here we are taking the case of Lotus Notes Emails, as we all know that we are totally depend on e-communication i.e. Emails. This article is fully dedicated to resolvin…
This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question