Removing empty lines in Rich Text field

Does anyone have any solutions for removing blank/empty lines from a Rich text field? In my case, I have to delete specific paragraphs from the rich text field, but after the delete, it seems to leave the carriage return + line feed (this is on Windows) in the field. The code for counting the blank lines works fine. But neither method of removing the extra lines seems to work. Here’s the code that is trying to do this.

I’m trying to do this in a way that preserves the remaining text and non-text items. Worst-case I’m willing to live with assuming the rtitem only contains text, but only if there is no other way.

Please help!

Sub CleanComment (doc As NotesDocument)

' Remove empty lines from Comment field

Dim noteField As NotesRichTextItem

Dim noteNav As NotesRichTextNavigator

Dim rtrange As NotesRichTextRange



Dim badStr$(1 To 3)

Dim goodStr$(1 To 3)

Dim cmnt$

Dim cleancmnt$



badStr$(1) = 	Chr(13) & Chr(10) & Chr(13) & Chr(10)

badStr$(2) = Chr(10) & Chr(10)

badStr$(3) = Chr(13) & Chr(13)

goodStr$(1) = Chr(13) & Chr(10)

goodStr$(2) = Chr(10)

goodStr$(3) = Chr(13)



' Count number of blank lines

cmnt$ = doc.GetItemValue("Comment")(0)



strPos% = Instr( cmnt$, badStr$(1) )

cnt% = 0

While Not( strPos%   = 0 )

	cnt% = cnt% + 1

	strPos% = Instr( strPos%+2, cmnt$, badStr$(1))

Wend

If ( cnt% > 0 ) Then

	Messagebox  "Found " & cnt% & " empty lines"

Else

	' No work to do

	Exit Sub

End If



' METHOD: Getting desperate -- try replacing the entire field.

' DANGER: This deletes all non-text items from the field.  No big deal?!

cleancmnt$ = Replace( cmnt$, badStr$,  goodStr$ )

' This does not work :-( -- changes the Comment field type to "Text" instead of "Rich Text".  May cause messups in Notes.

’ noteField.Values = cleancmnt$

' METHOD: Try richtext find and replace

Set noteField = doc.GetFirstItem("Comment")

cnt% = 0

For str_i% = Lbound(badStr$) To Ubound(badStr$)

	' Re-init range every time since FindAndReplace resets it

	Set rtrange = noteField.CreateRange

	cnt% = cnt% + rtrange.FindandReplace(badStr$(str_i%), goodStr$(str_i%), RT_REPL_ALL)		

Next

If ( cnt% > 0 ) Then

	Messagebox  "Method 1 Removed " & Str$( cnt% ) & " empty lines"

End If



' METHOD: Try richtext navigator functions

cnt% = 0

Forall srch In badStr$

	Set noteNav = noteField.CreateNavigator

	If noteNav.FindFirstString(srch) Then			

		Do 

			Call rtrange.SetBegin( noteNav )

			Call rtrange.SetEnd( noteNav )

			Call rtrange.Remove

			Call noteField.BeginInsert( noteNav )

			Call noteField.AddNewline( 1 )

			Call noteField.EndInsert

			cnt% = cnt% + 1

		Loop While noteNav.FindNextString(srch) 

	End If

End Forall

If ( cnt% > 0 ) Then

	Messagebox  "Method 2 Removed " & Str$( cnt% ) & " empty lines"

End If



Call noteField.Update

Call noteField.Compact

Call doc.Save( True, False )

End Sub

P.S. On first glance, the docs for Compact() method seemed to indicate that it would fit the bill, but so far it has done no compacting of any sort for me!

Subject: Removing empty lines in Rich Text field

I perhaps found a solution to this problem. It all depenjds on how a normal devaloper would think …A normal developer would think search for the first occurence of that string and replace it, search for the next one and replace it. until you do not find any further occurence.

What you will have to do is:

Search for the first occurence, replace it, update rtitem, rebuild the rtnav, rebuild the rtrange, search for the first occurence of the string again.

here is some code to prove this.

Set rtnav = rtItem.CreateNavigator

	Set rtrange = rtItem.CreateRange

'%REM

	If rtnav.FindFirstString("^") Then

		Do

			Call rtrange.SetBegin(rtnav)

			Call rtrange.SetEnd(rtnav)

			Call rtrange.remove

			Call rtitem.BeginInsert(rtnav)

			Call rtitem.addnewline(1)

			Call rtitem.EndInsert

			Call rtitem.Update

			Set rtnav = rtItem.CreateNavigator

			Set rtrange = rtItem.CreateRange

		Loop While rtnav.findfirststring("^")

	End If

Thomas