Saturday, May 10, 2014

Prepend a RichText Item with an another Richtext Item

One day, one of my colleague came to me and discussed about how to prepend a RichText field/item with a Text or with an another RichText context. And we found that there is no direct methods to achieve this functionality. When I searched for the solution in blogs and forums, they recommended to use RichTextNavigator class. But I decided not to use RichTextNavigator. While exploring, I found the below solution and liked to share this as well.

Solution to add something(Text/RichText Item) in the beginning of the Richtext field.

 //Declaration of object holders
 Dim item1 As NotesItem
 Dim rt1 As NotesRichTextItem
 Dim rt1_Temp As NotesRichTextItem
 Dim rt1_TempFinal As NotesRichTextItem 
 //Setting the item
 Set item1 = doc.GetFirstItem("body") 
 Set rt1 = doc.GetFirstItem("body") 
 //Creating temporary RT Item as a prepended RT Item
 Set rt1_Temp = doc.CreateRichTextItem("bodyTemp")
 Call rt1_Temp.AppendText("Hi foo, I have prepended an RT Item")
 Call rt1_Temp.AppendRTItem(rt1)
 
 //Removing old RT Item
 Call item1.Remove
 
 //Creating new RT Item in place of the same body richtext field
 Set rt1_TempFinal = doc.CreateRichTextItem("body")
 Call rt1_TempFinal.AppendRTItem(rt1_Temp)
 
 //Getting temporary item to remove
 Set item2 = doc.GetFirstItem("bodyTemp")
 Call item2.Remove
 
 Call doc.Save(True,False)

Hope this helps.

No comments:

Post a Comment