Copying attachment through Lotus Script attaches to the document instead of the rich text field

Please complete the details below then remove this line:


Domino/Notes Version: 12.0.2
Operating System: Windows Server 2019
Client (Notes, Nomad Web, Nomad Mobile, Android/iOS, browser version): 12.0.2


Problem/Query:

Hi,

I need to copy attachments from one rich text field in a document to a rich text field in another document. I used the below code but the attachment appears at the bottom of the new document rather than in the rich text field.

Sub Click(Source As Button)
Dim ws As New notesuiworkspace
Dim ses As New notessession
Dim db As notesdatabase
Dim uidoc As notesuidocument
Dim Nuidoc As notesuidocument
Dim cur As notesdocument
Dim fdoc As notesdocument
Dim rtitemA As NotesRichTextItem, rtitemB As NotesRichTextItem
Dim tempPath As String
Dim fileName As String
Dim obj As NotesEmbeddedObject

Set db = ses.currentdatabase
Set uidoc = ws.currentdocument
Set cur = uidoc.document

Set Nuidoc = ws.ComposeDocument(cur.ServerName(0),"abc.nsf","TestForm")
Call Nuidoc.Refresh    

Set fdoc = Nuidoc.document
Call fdoc.ComputeWithForm(True,False)


Set rtitemA = New NotesRichTextItem ( fdoc, "InsurArr")            
Set rtitemB = cur.GetFirstItem( "CPRAtt" )    
If ( rtitemB.Type = RICHTEXT ) Then
    Call rtitemA.AppendRTItem( rtitemB )
End If

Call Nuidoc.refresh    
Call fdoc.save(True, False)
Call Nuidoc.save
Call Nuidoc.close
Set Nuidoc = ws.EditDocument(False, fdoc)         

End Sub

Can you please assist me? I used various other methods like copyitemtodocument, extract attachment and embed etc but all seem to have the same behavior.

Thanks,

Satish.

I would try to create the new document back-end (and not directly front-end), then attach the file by any of the methods you mentioned. And then if possible the best is to save the doc before opening it front-end.
Editing Rich Text front-end has never been easy.

You may also try to use the method : NotesRichTextItem.Update . It may work to refresh the rtitem front-end.

Are you sure, that in one document the Richtextitem name is “InsurArr“ and in the other one it is “CPRAtt“?

And why do you create a new Richtext-Item in an existing document instead of taking the one that already is there?

If you are sure about the names of the richtext item then refactor the code like this:

'Alternative 1: refactor and create in backend, then bring to frontend

Dim otherDb as NotesDatabase
Set otherdb = New NotesDatabase( cur.ServerName(0), "abc.nsf")

Set fdoc = New NotesDocument( otherdb )
Call fdoc.ReplaceItemValue("Form","TestForm")
Set rtitemA = New NotesRichTextItem ( fdoc, "InsurArr")
Set rtitemB = cur.GetFirstItem( "CPRAtt" )
If ( rtitemB.Type = RICHTEXT ) Then
  Call rtitemA.AppendRTItem( rtitemB )
End If
'- optional: might work without the save line...
Call fdoc.Save(True, True, True)

Set Nuidoc = ws.EditDocument(True,fdoc)

'Alternative 2: Use the undocumented “Importitem” function that is used
' in “CoreEmailClasses” LotusScript-Library of HCL default mail template:

Set Nuidoc = ws.ComposeDocument(cur.ServerName(0),"abc.nsf","TestForm")
Call Nuidoc.GotoField("InsurArr")
Call Nuidoc.ImportItem(cur, "CPRAtt")

Hi @SatishBolla

The issue happens because you are mixing NotesDocument (backend) updates with a NotesUIDocument (frontend) that is already open or being composed. When you use AppendRTItem on a document that is currently activated in the UI, Domino often loses track of where the attachment belongs, so it sticks it at the very bottom of the form as a fallback. Here’s a simple LotusScript code that uses CopyItemToDocument method to preserve the complex formatting of the embedded object or attachment as it treats the RT item as a single unit:


  Dim ws As New NotesUIWorkspace
  Dim ses As New NotesSession
  Dim curdb As NotesDatabase
  Dim cur As NotesDocument ' Source
  Dim fdoc As NotesDocument ' Target
  Dim item As NotesItem
  
  Set curdb = ses.CurrentDatabase
  Set cur = ws.CurrentDocument.Document
  
  ' 1. Create target doc in background
  Set fdoc = New NotesDocument(curdb)
  fdoc.Form = "TestForm"
  
  ' 2. Grab the source item
  Set item = cur.GetFirstItem("CPRAtt")
  
  If Not item Is Nothing Then
      ' 3. Copy the item to the new document
      ' This copies it with the ORIGINAL RT field name ("CPRAtt")
      Dim newItem As NotesItem
' copy the item to the new ("InsurArr")
      Set newItem = item.CopyItemToDocument(fdoc, "InsurArr")

  End If
  
  ' 4. Save to "lock" the Rich Text positioning
  Call fdoc.ComputeWithForm(True, False)
  Call fdoc.Save(True, False)
  
  ' 5. Open the new form for the user
  Call ws.EditDocument(True, fdoc)

Regards,
TJ

Thanks Fabrice. It worked with the backend classes alone.

Thanks Tode. I was able to do the needful by removing the front end classes.

Hi TJ_Jabien,

Thanks for the tip. It worked.

Satish.