Hello,
I have used Webbrowser control in editable mode.I am using IHTMLTxtRange for find a word and replacing it with other word.I have to find and replace internally in my code.
Following is my code:-
Public Sub FindReplaceText(ByVal sfindText As String, ByVal sReplacetext As String)
Try
FindReplaceReset()
FindReplaceAll(sfindText, sReplacetext, False, False)
Catch ex As Exception
End Try
End Sub
Private Sub FindReplaceReset()
document = DirectCast(HTMLEditor.Document.DomDocument, IHTMLDocument2)
body = DirectCast(document.body, HTMLBody)
' reset the range being worked with
_findRange = DirectCast(body.createTextRange(), IHTMLTxtRange)
DirectCast(document.selection, IHTMLSelectionObject).empty()
End Sub
' replaces all the occurrences of the given string with the other
Private Function FindReplaceAll(ByVal findText__1 As String, ByVal replaceText As String, ByVal matchWhole As Boolean, ByVal matchCase As Boolean) As Integer
Dim found As Integer = 0
Dim replaceRange As IHTMLTxtRange = Nothing
Do
' find the given text within the find range
replaceRange = FindText(findText__1, matchWhole, matchCase)
' if found perform a replace
If replaceRange IsNot Nothing Then
replaceRange.text = replaceText
found += 1
End If
Loop While replaceRange IsNot Nothing
' return count of items repalced
Return found
End Function
'FindReplaceAll
' function to perform the actual find of the given text
Private Function FindText(ByVal sfindText As String, ByVal matchWhole As Boolean, ByVal matchCase As Boolean) As IHTMLTxtRange
' define the search options
Dim searchOptions As Integer = 0
If matchWhole Then
searchOptions = searchOptions + 2
End If
If matchCase Then
searchOptions = searchOptions + 4
End If
If Not _findRange.text Is Nothing Then
' perform the search operation
If _findRange.findText(sfindText, _findRange.text.Length, searchOptions) Then
' select the found text within the document
_findRange.[select]()
' limit the new find range to be from the newly found text
Dim foundRange As IHTMLTxtRange = DirectCast(document.selection.createRange(), IHTMLTxtRange)
_findRange = DirectCast(body.createTextRange(), IHTMLTxtRange)
_findRange.setEndPoint("StartToEnd", foundRange)
' text found so return this selection
Return foundRange
Else
' reset the find ranges
FindReplaceReset()
' no text found so return null range
Return Nothing
End If
Else
' reset the find ranges
FindReplaceReset()
' no text found so return null range
Return Nothing
End If
End Function
It works fine for all find and replace except if the word contains vbnewline.If in webbrowser control I enter
Hiii
Hello.
The above two words contain Enter key(i.e vbnewline) between them.If I pass followinf string to my Method it doesn't recognize the whole word and doesn't replace the word. I have called my method as
FindReplaceAll("Hii"+vbnewline+"Hello","HiiiHelloReplaced", False, False)
Can anyone tell me why is it so?