Wednesday, December 23, 2009

Basic validation for Rich Text Fields

When a trim is enough for checking whether a field contains a value or not, It is not that simple with Rich Text fields.

The following function shall help you with that to some extent.

Limitiations
1. By default, The handle for the rich text field can be obtained only when a document is saved

2. Any Images copied and pasted into the rich text field from the clipboard cannot be processed

I understand the above two points as a limitation with Lotus Notes. Please correct me if I am wrong

3. In addition to this function, you may have to write some code to check whether the rich text field contains text in it. Though that can be implemented along with this function, It has not been done at this point of time


'**********************************************************************************
'@Author : Karthikeyan A
'@Purpose : To inform the user if a rich text field contains embedded objects or not.
'@Note : This is for front end usage only and cannot be used with scheduled agents
'@Note : Images pasted from clipboard will not be processed. I understand it as a limitation of IBM Lotus Notes. Please correct me if I am wrong
'@Type : Function
'@Name : validateRichTextField
'@Param : richTextFieldName As String
'@Return: Boolean (has operation succeded, True stands for success and False stands for failure)
'**********************************************************************************

Function validateRichTextField(richTextFieldName As String) As Boolean

'mark the flow of control getting inside the current function
validateRichTextField=False

'declare all the necessary objects and variables required for further manipulation
Dim workSpace As New NotesUIWorkspace
Dim uidoc As notesuidocument

Dim rtitem As NotesRichTextItem
Dim currDoc As notesdocument
Dim rtnav As NotesRichTextNavigator

Dim noOfEmbeddedItems As Integer

'set the handle for the current document
Set uidoc = workSpace.CurrentDocument

'save the current document
Call uidoc.Save

'set the back end handle for the current document
Set currDoc =uidoc.Document

'set the handle for the rich text field with the name provided as the parameter
Set rtitem=currDoc.GetFirstItem(richTextFieldName)
'if the handle for the rich text field is not set then exit function
If rtitem Is Nothing Then
Exit Function
End If

'initialise the no of items found to 0
noOfEmbeddedItems=0

'set the handle for the navigator to navigate inside the rich text field and find the number of embedded items
Set rtnav = rtitem.CreateNavigator

'for all embedded items found in the field increase the count of the number of
' items found in the field except for the text portion
If rtnav.FindFirstElement(RTELEM_TYPE_DOCLINK ) Or _
rtnav.FindFirstElement(RTELEM_TYPE_FILEATTACHMENT ) Or _
rtnav.FindFirstElement(RTELEM_TYPE_OLE) Or _
rtnav.FindFirstElement(RTELEM_TYPE_SECTION) Or _
rtnav.FindFirstElement(RTELEM_TYPE_TABLE) Or _
rtnav.FindFirstElement(RTELEM_TYPE_TABLECELL) Then
noOfEmbeddedItems=noOfEmbeddedItems+1
End If

'if the number of items found in the field is zero then mark the flow of control moving out of the
'current function as false (marking failure) else true (marking success)
If noOfEmbeddedItems=0 Then
Exit Function
End If

'mark the flow of control moving out of the current function
validateRichTextField=True

End Function

2 comments:

  1. Thanks... I got the Idea of How to use NotesRichTextItem and NotesRichTextNavigator. But I need the names of the file attached to that Rich text field.

    ReplyDelete
  2. Thanks... Now I came to know how to use the NotesRichTextItem, NotesRichTextNavigator. But I need to get the names of the attachments in that rich text field

    ReplyDelete