Simple Find function on Webbrowser1 control?

Posted on 2003-02-24
Medium Priority
Last Modified: 2013-12-25
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!

Question by:FirstBorn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 14

Expert Comment

ID: 8013127

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.

Author Comment

ID: 8013280
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...



Accepted Solution

Rhaedes earned 200 total points
ID: 8016428
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,
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!


Author Comment

ID: 8017693
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!


Expert Comment

ID: 8018158
My pleasure :-)

Expert Comment

ID: 8018204
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,

Author Comment

ID: 8018279
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question