Copy RTItem from a doc to a new doc

Such a simple task. I got some code from this forum but none addressed creating a new document and copying a RTItem from an existing document. Both documents are based on the same form.

Sub FQJAgent

 '*********************************************************************************************

'** This agent will check FQJ - (Frequent Job Requests) and create

 '**  a new FQJ document if the correct number of days have elapsed

 '*********************************************************************************************

Dim Session As New NotesSession  

Dim ThisDB As NotesDatabase

Dim doc As NotesDocument

Dim newdoc As NotesDocument

Dim view As NotesView

Dim DateTime As New NotesDateTime("")

Dim impdate As New NotesDateTime("") 

Dim triggerdate As New NotesDateTime("") 

Dim targetdate As New NotesDateTime("") 

Dim DateVal As Variant

Dim Item As NotesItem

  '*

Dim impdatestring As String 

Dim adjustdays As Integer

	On Error Resume Next

	Set ThisDB = session.CurrentDatabase   

Set view = ThisDB.GetView("PMJOBS")

If view Is Nothing Then

End

End If

 	view.AutoUpdate = False

	Set doc = view.GetFirstDocument

While Not ( doc Is Nothing )

       	If statement to decide if this document is eligible

              to have a new document created !!!!

					Set newdoc = ThisDB.CreateDocument

newdoc.DateReceived = TargetDate.LSLocalTime

newdoc.TargetDate = TargetDate.LSLocalTime

newdoc.Form = "MWO"

Call newdoc.save(True,False)

Dim rtitemold As Variant

Dim rtitemnew As Variant

Set rtitemold = doc.GetFirstItem( "Body")

Set rtitemnew = newdoc.GetFirstItem( "Body")

If ( rtitemold.Type = RICHTEXT And  rtemnew.Type = RICHTEXT ) Then

Call rtitemnew.AppendRTItem( rtitemold )

End If

newdoc.comments = doc.comments(0)

newdoc.client = doc.client(0)

** etc more fields non-RTItem newdoc.FQJ = “YES”

Call newdoc.save(True,False)

doc.FQJ = "NO"

Call doc.save(True,False)

End If

End If          

     Set doc = view.GetNextDocument(doc)       

Wend

End Sub

Thanks…

Subject: Are you posting a question?

Subject: RE: Are you posting a question?

Yes, sorry if it is vague. The script I submitted would not copy a RTItem from the old doc( called doc ) to the new doc ( called newdoc ).From other posts it seems the problem is that the new doc is indeed brand new. That is why I tried putting a save after just populating a couple of items ( including the form ).

But to no avail.

Subject: RE: Are you posting a question?

What is happening when you run the code? Is a new document created but the rich text field is not copied across?

From just reading the code I am guessing that this line returns false for one or both of the conditions so it never copies the rich text item across

If ( rtitemold.Type = RICHTEXT And rtemnew.Type = RICHTEXT ) Then

Call rtitemnew.AppendRTItem( rtitemold )

End If

Run the code through in the debugger to see what happens. If the test condition never passes then split it up further so you know what exactly is causing the problem

If ( rtitemold.Type = RICHTEXT) then

msgbox “Old item is rich text”

end if

If (rtemnew.Type = RICHTEXT ) Then

msgbox “New item is rich text”

end if

if the second conition returns false then remove this condition and try

If ( rtitemold.Type = RICHTEXT) then

Dim rtitem As NotesRichTextItem

set rtitem = newdoc.CreateRichTextItem( “Body”)

Call rtitem.AppendRTItem( rtitemold )

end if

Subject: RE: Are you posting a question?

Thanks for your attention.Using debugger…

rtitemold shows ok after the Set ([EMBEDDEDOBJECTS] with the excel sheet showing and the text is in the TEXT and TYPE is 1).

The rtitemnew shows it is a [NOTESITEM] but no data .

The If statement falls thru to the Call rtitemnew.AppendRTItem( rtitemold ) but does nothing.

The new doc shows HASEMBEDED as False and the old doc shows True.

The new doc has no ‘Body’ field at all.

What is so difficult here. Is it about being a ‘New’ document or is it special handeling of RTItems?

Sorry to be such a pain.

Subject: RE: Are you posting a question?

I receive an object variable not set error when running similar code and testing forIf ( rtitemold.Type = RICHTEXT And rtitemnew.Type = RICHTEXT ) Then

because rtitemnew is not set to anything apart from [NOTESITEM]. You will not receive the error message because you are using the On Error Resume Next statement.

This continues the code at the line following the error. Without this statement the line

Call rtitemnew.AppendRTItem( rtitemold )

would never be reached

The problem is caused because of the different ways fields are set when creating a document in the front end or by lotusscript.

If you compose a document and then save it in the front end then all fields that are in the form are saved with the document even if they are not completed by the user. When you create a document using lotusscript only those fields that you explicitly set are saved to the document. So if you have 10 fields on a form but only set 3 of them in your script then only these 3 appear in the document.

The newdoc you have created and saved does not have a Body field so

Set rtitemnew = newdoc.GetFirstItem( “Body”)

will not work the way you are trying to use it.

What you need to do is to create the rich text field on newdoc

Dim rtitem As NotesRichTextItem

set rtitem = newdoc.CreateRichTextItem( “Body”)

Call rtitem.AppendRTItem( rtitemold )

Remove the following lines of code

Dim rtitemnew As Variant

Set rtitemnew = newdoc.GetFirstItem( “Body”)

and the test for

rtemnew.Type = RICHTEXT

You can also remove the first

Call newdoc.save(True,False)

Subject: RE: Are you posting a question?

this worked…

Set newdoc = ThisDB.CreateDocument

newdoc.DateReceived = TargetDate.LSLocalTime

newdoc.TargetDate = TargetDate.LSLocalTime

Dim rtitemold As NotesRichTextItem

Set rtitemold = doc.GetFirstItem( “Body”)

Dim rtitem As NotesRichTextItem

Set rtitem = newdoc.CreateRichTextItem( “Body” )

If rtitemold.Type = RICHTEXT Then

Call rtitem.AppendRTItem( rtitemold )

End If

newdoc.comments = doc.comments(0)

newdoc.client = doc.client(0)

plus this …after all fields populated

newdoc.Form = “MWO”

flag = newdoc.ComputeWithForm(False,False)

Call newdoc.save(True,False)

Thanks for all your help.