Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 904
  • Last Modified:

Simple Find function on Webbrowser1 control?

Is there a simple way to find text on a webbrowser control in VB6?

Something like: webbrowser1.document.all.find = text1.text

If so, is there a way to also replace this text displayed in the webbrowser1 control?
ex. webbrowser1.document.all.replace(variable, text2.text)

I do not want to use the common dialog find box, because the project needs this automated by searching keywords in a database and then changing the text after prompting user if they want to change the text And having the option to just change the text automatically.

Thanks in Advance!

  • 3
  • 3
1 Solution

Im not that good with the webbrowser control, but when I need to do something like that, I save to a temp file that I later "navigate2".

So everytime I change something, I save the textbox contents to a file and then load the file.
FirstBornAuthor Commented:
Hi Esopo,

I may be using a text file as a database, so it would probably disorganize things if I did that, though it is something I may look into further.

I figured out a way that I may be able to do a 'manual' search for a word or phrase, using:

I was thinking that I may have to start with creating 2 variables:

Dim intText as integer
dim strText as string

strText = RecordsetField
intText = len(Webbrowser1.Document.body.outerText)

then loop through the 'outertext' to find strText.
similar to:

now, one of my two major hurdles is accessing a .dbf file.  I'm so used to SQL Server 7 and M$ Access that I'm not used to using Visual FoxPro Tables...

If I could access the Visual FoxPro Table, I can set the strText to a specific Field's Recordset to complete this procedure.

My Second major Hurdle will be replacing the text I think the syntax will be similar to the WebBrowser1.Document.Write But I don't know the exact syntax or procedure for that...


While you can in theory get hold of the source of the html document, do a global replace and write the string into the browser, this is not a good way to go, simply because you may overwrite some of the html tags and attributes, as well as the text (image what would happen if you tried to replace the word 'body'...)
A much better approach is to establish a textrange over the body and work with that. The example below feed some Javascript into the webbrowser to replace all instances of the word in Text1 with the word in Text2.

Take a form, add the two textboxes, a webbrowser (webbrowser1) and a commandbutton. Paste the following into the form:

Private Sub Command1_Click()
TextReplace Text1.Text, Text2.Text, "2"
'The first argument is the text to be detected.
'The second is the text you want to change it into.
'The last parameter determines the type of match:
'0 Match partial words.
'1 Match backwards.
'2 Match whole words only.
'4 Match case.
End Sub

Private Sub TextReplace(ChangeFrom As String, ChangeInto As String, searchType As String)
myScript = "var rng = document.body.createTextRange();" + _
    "for (i=0; rng.findText('" + Text1.Text + "',1," + searchType + ")!=false; i++) {" & _
    "rng.text='" + Text2.Text + "';" & _
WebBrowser1.Document.parentWindow.execScript (myScript)
End Sub

Private Sub Form_Load()
WebBrowser1.Navigate2 ("about:blank")
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE

'Stick in a bit of text for example's sake
With WebBrowser1.Document
.write "<body>Now is the time for <b><i>all</i> good men</b> to come to the age of their party</body>"
End With

End Sub

Hope this helps,
Kindest regards,

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

FirstBornAuthor Commented:
ding ding ding ding ... and the winner is: Rhaedes

I tested this code successfully and seems to be EXACTLY What I'm looking for!

Thank You!

My pleasure :-)
Actually... I've just spotted a silly error: In the lines that put the JavaScipt string together (myScript) it should say 'ChangeFrom' instead of 'Text1.Text' and 'ChangeInto' instead of Text2.Text', since these are the variables passed to the function. (As it stands the sub ignores the variables passed to it and just uses what is in the textboxes.)

Kindest regards,
FirstBornAuthor Commented:
Either way, it worked beautifully!

Thanks Again!

Now, all I have to do is figure out how to access a FoxPro 6 dbf file in VB6 and I'll be well on my way to completing this project...


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now