Copying values from a profile document into current document. Hmm?

I am trying to create a button on a form that will copy the contents of a rich text field from a profile document to a rich text field on the existing, open document. I’ve muddled through the following script but it generates a “Type Mismatch” error. I think I may be confusing the doc with the uidoc. or something. Anyway, if anyone can point me in the right direction I’d appreciate it. Thanks…

Profile document is named “AP”

Rich text field in profile document is “Test”

Rich text field in current document is “Body”

Sub Click(Source As Button)

Dim session As New NotesSession

Dim ws As New notesuiworkspace

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim uidoc As notesuidocument

Dim ItemA As Variant

Set db = session.CurrentDatabase

Set uidoc=ws.currentdocument

Call uidoc.save

Set doc = db.GetProfileDocument("AP")

Set ItemA = doc.GetFirstItem("Test")

Call itemA.CopyItemToDocument(uidoc,"Body")

End Sub

Subject: Copying values from a profile document into current document. Hmm?

The problem is in the last line of your code…the CopyItemToDocument applies to NotesDocument and not NotesUIDocument.

Here’s the revised code:

Sub Click(Source As Button)

Dim session As New NotesSession

Dim ws As New notesuiworkspace

Dim db As NotesDatabase

Dim profileDoc As NotesDocument

Dim doc As NotesDocument

Dim uidoc As notesuidocument

Dim ItemA As Variant



Set db = session.CurrentDatabase

Set uidoc=ws.currentdocument

Set doc = uidoc.Document

Call uidoc.save



Set profileDoc = db.GetProfileDocument("AP")

Set ItemA = profileDoc.GetFirstItem("Test")

Call itemA.CopyItemToDocument(doc,"Body")

End Sub

Subject: RE: Copying values from a profile document into current document. Hmm?

Same code I just posted.

Subject: Great minds think alike!

Subject: Copying values from a profile document into current document. Hmm?

Thank you for you feedback. I’ve modified the code according to your suggestions and the type mismatch error did go away. However, the contents of the profile doc field are not being copied into the current document. I’ve checked to make sure i’m getting to the profile doc and that seems to be OK. I must be missing something else. Thanks for taking a look and any additional comments would be appreciated.

Here’s the problematic code that I’m using now:

Sub Click(Source As Button)

Dim session As New NotesSession

Dim ws As New notesuiworkspace

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim uidoc As notesuidocument

Dim ItemA As notesitem

Set db = session.CurrentDatabase

Set uidoc=ws.currentdocument

Call uidoc.save

Set doc = db.GetProfileDocument("AP")

'test to make sure you can get to the profile document by reading contents of field named test

Msgbox( doc.test(0))	

'copy value from rich text field (Template1) of profile doc into Body field of current document

Set ItemA = doc.GetFirstItem("Template1")

Call itemA.CopyItemToDocument(doc,"Body")

End Sub

Subject: RE: Copying values from a profile document into current document. Hmm?

Please check the revised code I posted. You’re copying the item from the profile document back into the profile document itself.

Alex

Subject: RE: Copying values from a profile document into current document. Hmm?

Yep, I see what you mean. I think I’ve been thrown off in my testing (and that doesn’t take much!) by the fact that the information from the profile document has actually been added to the document but I couldn’t see it. The following code creates a second body field on my current document instead of adding the rich text from the profile document to the existing body field. I’m afraid I don’t see how to update an existing body field in the current uidoc.

Sub Click(Source As Button)

Dim session As New NotesSession

Dim ws As New notesuiworkspace

Dim db As NotesDatabase

Dim profiledoc As NotesDocument

Dim uidoc As notesuidocument

Dim doc As NotesDocument

Dim ItemA As notesrichtextitem

Set db = session.CurrentDatabase

Set uidoc=ws.currentdocument	

Set doc=uidoc.document

Call uidoc.save

Set profiledoc = db.GetProfileDocument("AP")

Set ItemA = profiledoc.GetFirstItem("Test")

Call itemA.CopyItemToDocument(doc,"Body")

End Sub

Subject: RE: Copying values from a profile document into current document. Hmm?

Here’s revised code appending item from profile document to Body field of UI document…

Sub Click(Source As Button)

Dim session As New NotesSession

Dim ws As New notesuiworkspace

Dim db As NotesDatabase

Dim profiledoc As NotesDocument

Dim uidoc As notesuidocument

Dim doc As NotesDocument

Dim ProfileRTItem As NotesRichTextItem

Dim DocRTItem As NotesRichTextItem

Set db = session.CurrentDatabase

Set uidoc=ws.currentdocument

Set doc=uidoc.document

Call uidoc.save

Set profiledoc = db.GetProfileDocument(“AP”)

Set ProfileRTItem = profiledoc.GetFirstItem(“Test”)

If Doc.HasItem(“Body”) Then

Set DocRTItem = doc.GetFirstItem(“Body”)

Else

Set DocRTItem = New NotesRichTextItem(Doc, “Body”)

End If

Call DocRTItem.AppendRTItem(ProfileRTItem)

Call Doc.Save(True, True)

’ Reload & refresh the uidoc with changes from its

’ corresponding back-end document

If uidoc.EditMode Then

Call uidoc.Reload

Call uidoc.Refresh(True)

End If

End Sub

See how that goes.

Alex

Subject: RE: Copying values from a profile document into current document. Hmm?

Alex… thank you for helping me with this. I appreciate it. The code is doing almost all that I need right now. I ran into a bit of a problem with the uidoc.reload / uidoc.refresh not working as expected. I found this posting http://www-10.lotus.com/ldd/nd6forum.nsf/0/dd507a5be7cc2e4285256eec005f56ef and incorporated it into the last part of my script. I don’t know if there is a better way but right now I’m happy that its working. My working script is below which you will see is 99% what you provided. Thanks again!!

Mike

Sub Click(Source As Button)

Dim session As New NotesSession

Dim ws As New notesuiworkspace

Dim db As NotesDatabase

Dim profiledoc As NotesDocument

Dim uidoc As notesuidocument

Dim doc As NotesDocument

Dim ProfileRTItem As NotesRichTextItem

Dim DocRTItem As NotesRichTextItem



Set db = session.CurrentDatabase

Set uidoc=ws.currentdocument

Set doc=uidoc.document

Call uidoc.save



Set profiledoc = db.GetProfileDocument("AP")

Set ProfileRTItem = profiledoc.GetFirstItem("Test")

Set DocRTItem = New NotesRichTextItem(Doc, "Body")



Call DocRTItem.AppendRTItem(ProfileRTItem)

Call Doc.Save(True, True)





doc.SaveOptions = "0" 

Call uidoc.Close 

Set uidocNew = ws.EditDocument(True, doc, , , , True)

Delete uidoc 

End Sub

Subject: RE: Copying values from a profile document into current document. Hmm?

Glad to hear it’s working now. Yeah there’s always been a bit of a problem when updating back-end rich text fields and getting those updates into the UI.

Alex

Subject: Copying values from a profile document into current document. Hmm?

The copyItemToDocument is expecting a back end notes document (not a uidoc), something like this should work (not tested):

Dim session As New NotesSession

Dim ws As New notesuiworkspace

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim uidoc As notesuidocument

Dim ItemA As Variant

Dim ProfDoc As NotesDocument

Set db = session.CurrentDatabase

Set uidoc=ws.currentdocument

Set Doc = UIDoc.Document

Call uidoc.save

Set ProfDoc = db.GetProfileDocument(“AP”)

Set ItemA = ProfDoc.GetFirstItem(“Test”)

Call itemA.CopyItemToDocument(doc,“Body”)