Copy RichTextItem in another with attachment

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

Subject: Copy RichTextItem in another with attachment

Make sure you check that the replica is accessible by doing:If Not linkDb.isOpen() Then

Call linkDb.open("", "")

End If

If Not linkDb.isOpen() Then

' Handle that the user/agent is not authorized in that database, or that it doesn't exist

End If

  • Johan

Johan Känngård

http://dev.kanngard.net

Subject: RE: Copy RichTextItem in another with attachment

Thanks Johan. Of course I will insert this check in my code (best practice!), but the problem is that doc.save acts on a document already present in the current db (i.e. the db executing the agent) that is of course opened…

Probably I miss something or there is something that I don’t know :slight_smile:

G.

Subject: RE: Copy RichTextItem in another with attachment

Maybe a bug? Try checking (logging) if doc.parentDatabase is ok/open right before you save the document.

  • Johan

johan@kanngard.net

http://dev.kanngard.net

Subject: RE: Copy RichTextItem in another with attachment

Moreover, I performed this test:

  1. I identified a document for which such event occours

  2. I modified the agent in order to import only one attachment per time (the first doclink encountered) and save the current doc

  3. I run the agent several times until all the attachment are imported

It works. Thus my problem is related in collection management (or the RichTextNavigator, or the item obtained from forall statement)

Any suggestion?

G

Subject: RE: Copy RichTextItem in another with attachment

I logged as you suggest: the db is opened just before the doc.save statement.

I think that probably the problem is related to how Domino manage the attachment and richtextitem.

G.