Link to home
Start Free TrialLog in
Avatar of Jooger
JoogerFlag for Ireland

asked on

prefilling contents of a richtext field from a field in another doc

Hi

When I create a new doc I need to pre fill the contents of an RTF with some user defined formatted content ( eg tables and text of varying fonts) which is stored in a cfg doc field.

Ideally, I like to store it as an attachment in a cfg doc field, then when required open it, copy it and paste it all into the field of the new document. I assume I would need to use some VBA for this.

To save time I've tried just storing the content as is, and copying it directly to the new doc from within the postOpen event of the new doc but it doesn't work
ASKER CERTIFIED SOLUTION
Avatar of Bill-Hanson
Bill-Hanson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jooger

ASKER

Hi Bill

I need to popuplate the RTF in the new doc while I'm in PostOpen and the doc is a new document, so that the field is always pre-filled for the user and there are many other fields in the document, not just the RTF I need to write to.

I need to select & copy all the contents of a RT field in the config doc & copy it all into the target Rich Text Field.

I tried copying the source field to the target doc with notesdoc.CopyItem() but it didn't work.

I suppose the problem is the same as copying content from one RTF to another in the same document.

how can I use LotusScript to select all contents of a rich text field and paste it into another?
>> "I need to popuplate the RTF in the new doc while I'm in PostOpen"

PostOpen is a difficult place to work with rich text.  The reason is that RichText needs to be saved to the backend before the document is open.

It is possible, however, to open the config document in edit mode, select all content in a RT field, copy that to the clipboard, then close the config document, and paste the contents of the clipboard into the new document's RT field, but this is prone to errors, plus it destroys anything that the user had on the clipboard (a sure way to tick off your users).  If you still want to do it this way, here's the basic idea:

1) In PostOpen, get a handle to your config document.

Dim docConfig as NotesDocument
Set docConfig = ...

2) Edit the config document using NotesUIWorkspace.EditDocument.

Dim uiConfig as NotesUIDocument
set uiConfig = ws.EditDocument(True, docConfig)

3) Navigate to the source RT field, select all, and copy to clipboard.

Call uiConfig.GotoField("YourFieldName")
Call uiConfig.SelectAll
Call uiConfig.Copy

4) Close the config document, but don't prompt user to save.

docConfig.SaveOptions = "0"
Call uiConfig.Close

5) Paste the contents into your new document.

Call Source.GotoField("YourFieldName")
Call Source.Paste

That should do it, but I still prefer working on RT in the backend.
Avatar of Jooger

ASKER

Bill thanks for the step by step specifics.

yr point about overwriting any user content in the clipboard is well made.
Using uidoc.editdocument in this way would also give screen flicker.
So, not ideal.

So, in PostOpen of the new doc, I initialise the other fields that need it and when the last thing I do is save the doc, copy the item from the config doc and then reopen the new doc - which is not now a new doc.
All the fields, inc my RTF should now be initialised.

Would QuerySave() & PostSave() trigger if a doc is saved within PostOpen()?
These events add control info to the doc if it's a new doc.
A way around this would be to add a temporary IsNewField to the doc but that adds complexity.

I'll give this a tryand let u know what happens.
>> "Would QuerySave() & PostSave() trigger if a doc is saved within PostOpen()? "

Only if you do a front-end save using NotesUIdocument.Save.  If you do a back-end save using NotesDocument.Save, no form events will be fired.

One problem that you might have here is that reloading or refreshing the form will not display the changes made to the rich text in the back-end.  You have to actually close and re-open the ui document (close the uidoc, modify the rich-text, save the back-end doc, re-open the uidoc).

>> "A way around this would be to add a temporary IsNewField to the doc but that adds complexity."

You may need to do this anyway to prevent the re-initialization of the rich text.
Avatar of Jooger

ASKER

Hi Bill

apologies for the delay.

It does work. The code below works. ( working code  copied and simplified )

I have to manage the IsNewDoc elsewhere eg QuerySave()

Sub Postopen( Source As Notesuidocument)
        dim doc as NotesDocument
.
.
       
        set doc = Source.Document

      bIsNewDoc = Source.IsNewDoc or doc.IsNew(0) = ""
      If bIsNewDoc Then
            Dim docCfg As NotesDocument
            Dim rtItem As NotesRichTextItem
            
            doc.IsNew = "InitDone"
            
            Set docCfg = GetConfigDoc( db)
            
            Set rtItem = docCfg.GetFirstItem( "FieldX")
            Call rtItem.CopyItemToDocument( doc, "")
            
            Set rtItem = docCfg.GetFirstItem( "FieldY")
            Call rtItem.CopyItemToDocument( doc, "")
            
            Call doc.Save( True, True)
            Call Source.Close
            Set Source = ws.EditDocument( , doc)
            Set doc = Source.Document
      End If

End sub