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.

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??
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Problem "Can you help me recover my changes?  I double-clicked the attachment, made changes, and then hit Save before closing it.  But when I try to re-open it, my changes are missing!"    Solution This solution opens the Outlook Secure Temp Fold…
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now