Problem:How copy attachments from a richtextitem in a docA to another richtextitem in a docB, when you have several documents, with more than one richtextitem, everyone with different doclink?
Solution:
a loop on the documents selected
a loop on the documents items (searching for richtextitem)
a loop on the doclinks in each richtextitem
retrieving the attachment in every foreign document linked by each doclink in each richtextitem in each document selected.
(code follows)
This sometimes doesn’t work reporting a notes error (db not yet opened - no errorcode is present) on doc.save true,true statement.
Any suggestions?
TIA
G.
Code:
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim risposta As Integer
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Dim rtnav As NotesRichTextNavigator
Dim link As NotesRichTextDoclink
Dim rtitem As NotesRichTextItem
' open log database
Dim currentLog As New NotesLog( "Agent Log" )
Call currentLog.OpenNotesLog( "", "agentlog.nsf" )
' reference to the current database
Set db = session.CurrentDatabase
' confirm request
risposta = Msgbox("Import all files for selected document (" & _
db.UnprocessedDocuments.Count & ")?",MB_YESNO + MB_ICONQUESTION, "Confirm request")
If risposta=IDNO Then Exit Sub
' LOG: number of documents to be processed
currentLog.LogAction("Documenti da processare: " & db.UnprocessedDocuments.Count)
' cicle on selected documents
Set collection = db.UnprocessedDocuments
Set doc = collection.GetFirstDocument()
While Not(doc Is Nothing)
' LOG: nth document
currentLog.LogAction("Doc UID: " & doc.UniversalID & ", type: " & doc.tipo_doc(0) & ", n°: " & doc.numero_doc(0))
' cicle on document items
Forall item In doc.items
' check if right field type
If item.type=RICHTEXT Then
' setting navigator and retrieve the first doclink (if exists)
Set rtnav = item.CreateNavigator
Set link = rtnav.GetFirstElement(RTELEM_TYPE_DOCLINK)
' cicle on all doclink in the richtextitem
While Not(link Is Nothing)
' LOG: nth link
currentLog.LogAction("Link: " & link.ServerHint & "\" & link.DBReplicaID & "\" & link.ViewUnID & "\" & link.DocUnID)
' check if the doclink links to the current database
If link.DBReplicaID<>db.ReplicaID Then
' opening the remote db
Dim linkDb As New NotesDatabase("", "")
If linkDb.OpenByReplicaID(link.ServerHint, link.DbReplicaID) Then
' opening succeeded
Dim linkDoc As notesdocument
Set linkDoc = linkDB.GetDocumentByUNID(link.DocUnID)
' check for the item containing the attachment to be copied in the current database
Set rtitem = linkDoc.GetFirstItem( "Body" )
' copy of the richtextitem and remove the doclink
Call item.AppendRTItem( rtitem )
link.Remove
doc.Save True, True
' LOG: nth link
currentLog.LogAction("Exit: link processed and removed")
Else
' LOG: nth link
currentLog.LogAction("Exit: link unprocessed - db connection failed")
End If
Else
' LOG: nth link
currentLog.LogAction("Exit: link unprocessed - referencing current db")
End If
' ask to the navigator for the next doclink element
Set link = rtnav.GetNextElement(RTELEM_TYPE_DOCLINK)
Wend
End If
End Forall
' ask to the collection for the next document to be processed
Set doc = collection.GetNextDocument(doc)
Wend
Call currentLog.Close
End Sub