?
Solved

Webbrowser Control Find / Search / Replace Function...

Posted on 2003-03-22
12
Medium Priority
?
377 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
[X]
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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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