• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

Lotusscript, sending attachments from one database to another

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 destdoc.save(True, False)
            
            Set sourcedoc = col.getnextdocument(sourcedoc)
      Wend
      
End Sub
0
tashsmith
Asked:
tashsmith
  • 7
  • 3
  • 2
  • +1
3 Solutions
 
Sjef BosmanGroupware ConsultantCommented:
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?
0
 
tashsmithAuthor Commented:
Attachments are not being transferred, they are attached to the document in an rtf field. We now need them to transfer as well.
0
 
madheeswarCommented:
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.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
tashsmithAuthor Commented:
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?
0
 
marilyngCommented:
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")
0
 
Sjef BosmanGroupware ConsultantCommented:
Indeed, the same you already did for the Body field??
0
 
tashsmithAuthor Commented:
ok guys thanks, gonna give that a go...
0
 
tashsmithAuthor Commented:
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.

http://www-1.ibm.com/support/docview.wss?&uid=swg21197459

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?


Workaround

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
If o.type=EMBED_ATTACHMENT Then
Call o.extractfile("C:\" + o.source)
Call RTtarget.embedobject(Embed_Attachment, "", "C:\" + o.source)
Kill "C:\" + o.source
End If
End Forall



0
 
tashsmithAuthor Commented:
I ran the script using Notes 7 and am still experiencing the same problems, have you guys come across this problem?
0
 
marilyngCommented:
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

0
 
tashsmithAuthor Commented:
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
0
 
marilyngCommented:
tashsmith,

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.
0
 
tashsmithAuthor Commented:
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...

0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 7
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now