Home > Domino Tips > Developer > LotusScript > Modify a rich text field in the UI without having to save and re-open
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

LOTUSSCRIPT

Modify a rich text field in the UI without having to save and re-open


Bryce Berry
07.13.2004
Rating: -4.02- (out of 5) Hall of fame tip of the month winner


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


View member feedback to this tip.

One big issue with rich text fields is that "Modifications made to rich text items on the back-end document do not appear on the current document until it is closed and reopened." This means that any code you might have in the UI accessing a rich text field (create tables, append doc links, etc.) won't immediately show up without saving and re-opening.

Until now, either you saved and re-opened the back-end doc to reflect the changes to the rich text field (which meant that you also had to incorporate some sort of clean-up code if a user chose to cancel the save) or you don't reflect the changes in the UI, which can cause some user confusion ("yes, you did make the changes but you have to save and re-open the doc to see them").

With the following code, you can modify any rich text fields and reflect those changes in the UI without being forced to save the document. In this example, I am appending a document link but I have tested the code for creating a table as well. The user may notice a slight flicker as the document is closed and re-opened.

One other application for this code is Computed Subforms. In the Designer Help, it is stated that "Subform formulas cannot be refreshed while the document is open." This method of re-initializing the document in memory allows the Computed Subform to change.

Sub Click(Source As Button)
Dim s As New NotesSession
Dim ws As New NotesUIWorkspace
Dim thisdb As NotesDatabase
Dim coll As NotesDocumentCollection 
' collection returned from
PickListCollection
Dim otherdoc As NotesDocument 
' this is the doc to create a link to
Dim thisdoc As NotesDocument 
' the new doc that the link is being added to
Dim rtitem As NotesRichTextItem 
' required for AppendDocLink
Dim nitem As Variant 
' used to get a handle on the 
NotesRichTextField
Dim olduidoc As NotesUIDocument 
' the original instance of the uidoc
Dim newuidoc As NotesUIDocument 
' the new instance of the uidoc after the doc
link has been added

Set thisdb = s.CurrentDatabase
Set olduidoc = ws.CurrentDocument 
' current uidoc
Set thisdoc = olduidoc.Document 
' doc in memory but hasn't been saved yet

' select the doc to link to
Set coll = ws.PickListCollection
( PICKLIST_CUSTOM, False, thisdb.Server,
thisdb.FilePath, "luProject", 
"Project List", "Please select a project." )

' if a doc isn't selected exit
If coll.Count = 0 Then
Messagebox "User canceled" ,,
"No project was selected."
Exit Sub
End If

' get the doc to link to
Set otherdoc = coll.GetFirstDocument

' grab some values from that doc
thisdoc.ProjectName = 
otherdoc.ProjectName
thisdoc.CustomerName = 
otherdoc.CustomerName

' get the RichTextField in the 
current uidoc 
Set nitem = 
thisdoc.GetFirstItem( "ProjectDocLink" )

' add the doc link
' NOTE: this is being done to
 the backend doc that exists in memory
If ( nitem.Type = RICHTEXT ) Then
Set rtitem = nitem
Call rtitem.AppendDocLink(otherdoc,
 "Original Doc Link", "")
Call rtitem.Update
End If

' set the SaveOptions field so that 
when the uidoc is closed, 
the user won't be asked to save
thisdoc.SaveOptions = "0"

' close the uidoc. It won't actually 
happen until the code is finished executing
Call olduidoc.Close(True)

' create a new uidoc and open the
 backend doc that is still in memory with 
added doc link
Set newuidoc = ws.EditDocument(True, thisdoc)

' delete the reference to the old uidoc
' this is necessary because the 
code below affects it if left in memory
Delete olduidoc

' re-associate the variable with the backend doc
' have to do this because the 
olduidoc reference was deleted
Set thisdoc = newuidoc.Document

' remove the SaveOptions field
 so the doc can be saved
Call thisdoc.RemoveItem( "SaveOptions" )

End Sub 

MEMBER FEEDBACK TO THIS TIP

This works well, but the document you are working with must have been saved first. If it has not been saved, when the line of code shown below runs, you will get an error because the field is not actually a rich text field element yet. It will only be that after the document has been saved at least once.

If ( nitem.Type = RICHTEXT ) then you will receive this error if the document has not been saved:

Figure 1

—Steve H.

******************************************

I have tried to use to code to embed an attachment in the rich text field body, but I can not get rid of the field save options.

Sub Click(Source As Button) 
Dim s As New NotesSession 
Dim ws As New NotesUIWorkspace 
Dim thisdb As NotesDatabase 
Dim coll As NotesDocumentCollection 
' collection returned from 
        
Dim otherdoc As NotesDocument 
' this is the doc to create a link to 
Dim thisdoc As NotesDocument 
' the new doc that the link is being 
added to 
Dim rtitem As NotesRichTextItem 
' required for AppendDocLink 
Dim nitem As Variant 
' used to get a handle on the 
NotesRichTextField 
Dim olduidoc As NotesUIDocument 
' the original instance of the uidoc 
Dim newuidoc As NotesUIDocument 
' the new instance of the uidoc after 
the doc link has been added 
        
Const SCANFILEPATH$=
{C: \ Tempscan.jpg} 
Set thisdb = s.CurrentDatabase 
Set olduidoc = ws.CurrentDocument 
' current uidoc 
Set thisdoc = olduidoc.Document 
' doc in memory but hasn't been saved 
yet 
        
        
Set nitem = thisdoc.GetFirstItem( "Body" ) 
If ( nitem.Type = RICHTEXT ) Then 
Set rtitem = nitem 
Set object = rtitem.EmbedObject 
( EMBED_ATTACHMENT, "",        
SCANFILEPATH ) 
Call rtitem.Update 
End If 
        
thisdoc.SaveOptions = "0" 
Call olduidoc.Close(True) 
Set newuidoc = 
ws.EditDocument(True, thisdoc) 
        
Delete olduidoc 
Set thisdoc = newuidoc.Document 
Call thisdoc.RemoveItem( "SaveOptions" )         
        
End Sub

—Anonymous

******************************************

You can try modifying the code as follows:

Set thisdoc = olduidoc.Document 
' doc in memory but hasn't been saved 
yet

' if the doc is new, adds a 
new RichTextField in the current doc If 
thisdoc.IsNewNote Then
Set rtitem = 
thisdoc.CreateRichTextItem( "Body" ) End If

Set nitem = thisdoc.GetFirstItem( "Body" )

Also, if the form you are working with is not the default form in the database, you should also add a Computed Form field to the form so that when the code closes and re-opens the document, it knows what form to use.
— Bryce Berry, tip author

******************************************

I thought this tip would be the thing, but I then found an easier way. I hope this is useful. Also, I'm using VBA / Excel.

On Error Resume Next 
        MailDoc.Send False, "" 
On Error GoTo 0 
MailDoc.SendTo = SendToAdr 
Set workspace = 
CreateObject("Notes.NotesUIWorkspace") 
Call workspace.EDITDOCUMENT
(True, MailDoc, True).GOTOFIELD("Body") 

I send the doc to an empty/blank address, and trap and ignore the error. When I create the uidoc, my body appears, and I didn't have to save the doc first.
—Michael R.

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Bryce Berry. Please let others know how useful it is via the rating scale above. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
LotusScript
Create a non-specified date picker with LotusScript code
LotusScript accesses clipboard to view copied Notes documents
LotusScript action button manages Lotus Notes mail files
LotusScript sorts a Lotus Notes document collection
Display Lotus Notes user group membership details in a tree view
Alternate version of @Command forwards subform via LotusScript
Process large arrays in Notes forms without undue coding or testing
How to use LotusScript to modify a Lotus Notes view selection
Display a custom message box using a LotusScript-generated button
Debug Lotus Notes documents using extracted data

LotusScript
Customize the principal field of outgoing email messages
Create a non-specified date picker with LotusScript code
Resolve Notes 8 migration error: 'Database has not been opened yet'
LotusScript accesses clipboard to view copied Notes documents
LotusScript action button manages Lotus Notes mail files
LotusScript sorts a Lotus Notes document collection
Display Lotus Notes user group membership details in a tree view
Alternate version of @Command forwards subform via LotusScript
Process large arrays in Notes forms without undue coding or testing
How to use LotusScript to modify a Lotus Notes view selection

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts