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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For users on the Lotus Notes 8 Standard client, this article provides information on checking the Java Heap size and adjusting it to half of your system RAM in attempt to get the Lotus Notes 8.x Standard client to run faster.  I've had to exercise t…
Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

697 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