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

x
?
Solved

Webbrowser Control Find / Search / Replace Function...

Posted on 2003-03-22
12
Medium Priority
?
391 Views
Last Modified: 2010-04-07
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:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_20527392.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!

:)
0
Comment
Question by:FirstBorn
  • 6
  • 6
12 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8186635
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8186651
using createtextrange you could easily change its .text property where the text is found.
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8186730
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.

:)
0
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!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8187181
Java?, what java? it is pure vb.
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8187264
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.

:)
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8187548
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.

:)
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 400 total points
ID: 8187643
Well, we don't understand each other.
Why do you need a script anyway?
If you have a webbroser control in you VB program (is a VB program, right?)

try with this:
Option Explicit

Dim WithEvents IE As InternetExplorer
Dim WithEvents IEDoc As HTMLDocument
Sub SearchReplace(sTxt2Search As String, sText2Chg As String)
Dim txtrng As IHTMLTxtRange
Set txtrng = IEDoc.body.createTextRange 'IEDoc.body.innerText
txtrng.findText sTxt2Search
txtrng.Text = sText2Chg
End Sub

Private Sub Command1_Click()
SearchReplace "points", "richie"

End Sub


Private Sub Form_Load()
Set IE = New InternetExplorer
With IE
   .navigate "www.experts-exchange.com"
   .Visible = True
End With
End Sub


Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
   Set IEDoc = IE.document
   Command1.Enabled = True
End If
End Sub

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8187652
regarding count, there is a paramater of find method that you could use.
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8187792
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:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_20527392.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!

:)
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8188015
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.

:)
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8188912
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!

:)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8189792
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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

571 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