Link to home
Start Free TrialLog in
Avatar of FirstBorn
FirstBornFlag for United States of America

asked on

Webbrowser Control Find / Search / Replace Function...

Hi Everyone!

I am looking for a way to do perform a search and replace on a webbrowser control.  I need to also just find the text on the webbrowser and set it to a variable (string) without replacing the text.

I had a question answered in this thread but I don't want to use a script for this procedure:
https://www.experts-exchange.com/questions/20527392/Simple-Find-function-on-Webbrowser1-control.html

Here's EXACTLY What I want to do:

1.  Find any criteria (outer text?) on a webbrowser (not the HTML, but the actual Page loaded and displayed on the webbrowser)
2.  Prompt the user if any criteria has been found... (I can do this myself because I know what procedure needs to be run on vbyes or no... )
3.  Replace text found in #1 above.
4.  Pass the changes back to a richtextbox, but in HTML (of which I already have the code for this part and it is working successfully working.)

So, the question I'm specifically asking is:
How Do I Find Text on a loaded webbrowser control (outer text?) and then how could I Replace the text (criteria found on the Outer Text) of the webbrowser control?

Thank You in Advance!

:)
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

See if this could help to start (sorry, i have no time do a complete job)
http://www.angelfire.com/realm/vb-shared/IE_WB_DOM_tips.htm#selall
using createtextrange you could easily change its .text property where the text is found.
Avatar of FirstBorn

ASKER

Hi Richie,

I've done this (createtextrange), but it uses Java scripting in order to make it work... I Do Not want to use scripting for this procedure... Is there a way to do this Without using scripting?  If Not, what other way can I search for criteria and then replace if necessary?

Thanks.

:)
Java?, what java? it is pure vb.
Hi Richie,

If you do a search on google.com and microsoft (msdn and knowledgebase) you'll find every reference made to createtextrange is JScript or JavaScript... My procedure has the following code of which is a SCRIPT... I Do Not want to use the script and need to do this where I can just do a search and then opt to replace.

Dim ChangeFrom As String, ChangeInto As String, searchType As String
ChangeFrom = "test1"
ChangeInto = "test2"

myScript = "var rng = document.body.createTextRange();" + _
   "for (i=0; rng.findText('" + ChangeFrom + "',1," + searchType + ")!=false; i++) {" & _
   "rng.text='" + ChangeInto + "';" & _
   "rng.collapse(false);}"
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Loop
WebBrowser1.Document.parentWindow.execScript (myScript)

Is there anything like in a syntax below that will do this without having to run a script as stated in the first thread of this original question?

What I'm looking for would be similar to:
(I know the syntax is incorrect, but I'm looking for something in this format)
---
WebBrowser1.Document.Body.OuterText.Find (Criteria to search for)
and
WebBrowser1.Document.Body.OuterText = replace(WebBrowser1.Document.Body.OuterText,"blah","Blah Blah",,,vbTextCompare)
---

Thanks.

:)
k, Richie, Here's a question regarding your response above:
"using createtextrange you could easily change its .text property where the text is found."

How would I be able to:
1. Count how many instances have been found of the criteria that I'm searching for using your 'createtextrange' procedure that I have listed in my code above?

2. prompt user if an instance is has been found of criteria being searched?

3. replace instance(s) if user wants to replace the criteria found?

I will bump up the points on successful completion of testing of your code.

Thanks.

:)
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
regarding count, there is a paramater of find method that you could use.
Hi Richie,

Yep, it's a VB6 App... :)

I am getting an error: "430   Class does not support Automation or does not support expected interface"

on the following line:
Sub SearchReplace(sTxt2Search As String, sText2Chg As String)
Dim txtrng As IHTMLTxtRange
Set txtrng = IEDoc.body.createTextRange 'IEDoc.body.innerText <----- Error

I guess we're not 'understanding each other' because the previous code (mentioned above about 3 threads) uses a script that I've been using and is listed more clearly (as mentioned in the first thread of this question:
https://www.experts-exchange.com/questions/20527392/Simple-Find-function-on-Webbrowser1-control.html

After searching google.com, ms knowledgebase, and msdn, it had appeared that using scripting was the only way to perform my function... therefore, the confusion probably stems on my lack of knowledge of using the webbrowser control... :)

Thanks!

:)
Hi Richie,

k, I found it!
I'm using this code:
With WebBrowser1.Document
     Dim TxtRng As IHTMLTxtRange
     Set TxtRng = .body.createTextRange
     If TxtRng.findText("Search String Goes Here!") = True Then
          TxtRng.Select
          TxtRng.scrollIntoView True
      Else
          MsgBox "Text not found", vbInformation, App.EXEName
      End If
End With
This is EXACTLY What I'm looking for in Part 1. of the question.
What would I need to plug into the code above to count instances of search string found AND What code would I use to replace the search string with new criteria?

Thanks!

:)

Since I found this code using the premium search as "IHTMLTxtRange" as the search string thanks to your code, I'm boosting this up to 100 points and will boost it more with your help on my most recent questions.

:)
Hi Richie,

Although You didn't directly give me the code I was looking for, You have opened up a new 'arena' by helping me find the code (without the scripting) ... :)

Therefore, I'm going to bump this up and award you 150 points for indirectly answering my question.

Thank You VERY Much for your Help!  It is Greatly Appreciated!

:)