?
Solved

Count Text in Webbrowser Control...

Posted on 2003-03-22
17
Medium Priority
?
296 Views
Last Modified: 2010-04-07
I'm looking to count a string value displayed on a webbrowser control.

ex. The webbrowser displays the HTML Page, then, I need to search for a value (text1.text) and count how many times it appears in the browser.

The current code I am using does not pick up all of the values due to some of the values are within HTML Tags... Here are options that I have tried:

numTrigger = InStr(TriggerCountPosition, WebBrowser1.Document.documentElement.innerText, strText)
If numTrigger > 0 Then
intTriggerCount = intTriggerCount + 1
TriggerCountPosition = TriggerCountPosition + numTrigger
' etc...
also Tried:
WebBrowser1.Document.documentElement.OuterText
WebBrowser1.Document.Body.innerText
WebBrowser1.Document.Body.OuterText

The count picks up 5 for the documentElement.(both Outer and InnerText)
and count = 6 for the document.Body.(Outer and InnerText)
There are actually a lot more that should be found, however, I'm thinking that the html tags must somehow be blocking the (string) value from being picked up.

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
  • 10
  • 7
17 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8189810
Hi, see if there is no frames in the document. The count should be the same, not diferent.
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8190228
Hi Richie,

First, Did You get this?
http://search.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20559946.html

Second,
Shouldn't the outerText/InnerText pick up all of the values, regardless of whether it was in a frame or not?
I'm not using frames on the testing, however, there are a lot of <div></div> tags on the Testing HTML Document that I'm using.  There are also numerous font formatting tags within the document.  Would this have any affect on the search/finding of criteria?  I would think that the criteria (text being searched) should be found regardless...

Thanks.

:)

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8190668
Take a look at this:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20297499.html

By the way, did you see the links posted in the other Q?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

 
LVL 1

Author Comment

by:FirstBorn
ID: 8190796
Hi Richie,

Another Question at the very bottom...

The link that you have provided:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20297499.html is the reason why you were given the "A" and the points on the last question... 'indirectly' was the word I used for the explanation why you were awarded the points, I believe... :)

Actually, I found a way to get the proper count:
With WebBrowser1.Document
    Set TxtRng = .body.createTextRange
    numTrigger = 0
    lngTest = 1
    TriggerCountPosition = 1
    strText = "Text"
    If InStr(TriggerCountPosition, TxtRng.Text, strText, vbTextCompare) > -1 Then
        Do
            lngTest = InStr(TriggerCountPosition, TxtRng.Text, strText, vbTextCompare)
            If lngTest <> 0 Then
                numTrigger = numTrigger + 1
                TriggerCountPosition = lngTest + 1
            Else
                Exit Do
            End If
        Loop
    End If
    MsgBox numTrigger
End With

I have no clue why it wasn't working with the other code that I was using. However, this is the most stable code used and tested.

I saw the links that you gave me in the other Q, however, I'm going to have to actually click the links after I've plugged in my tested code into my project, thanks! ... :)

Don't forget to pick up your extra points:
http://search.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20559946.html
I didn't bump up the points on the last question, so I had to add that question to pass to you...


Here's the Question at the Very Bottom:

After performing the search, I am going to be replacing the criteria searched with, let's say, text1.text ...
ex. Txtrng.Text = Replace(txtrng.text,strText,strReplace)
(is that the proper syntax? Or, what would I use to replace the text on the webbrowser... the design mode = "On" already, so I know I can do it manually...)

After I replace the text, I will need to pass the changes back to a RichTextBox on another form.  Would the changes be seen if I use the following code?
Code:
gstrSource = WebBrowser1.Document.documentElement.outerHTML
frmHTML.rtfContent.Text = gstrSource
frmHTML.Visible = True
Unload Me

Thanks.

:)
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8190807
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8190908
After I replace the text, I will need to pass the changes back to a RichTextBox on another form.  Would the changes be seen if I use the following code?
Code:
gstrSource = WebBrowser1.Document.documentElement.outerHTML
frmHTML.rtfContent.Text = gstrSource
frmHTML.Visible = True
Unload Me


is this working?

0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8190982
Actually, This part does work:
gstrSource = WebBrowser1.Document.documentElement.outerHTML
frmHTML.rtfContent.Text = gstrSource
frmHTML.Visible = True
Unload Me

How would I perform the following: (proper syntax?)
After performing the search, I am going to be replacing the criteria searched with, let's say, text1.text ...
ex. Txtrng.Text = Replace(txtrng.text,strText,strReplace)
(is that the proper syntax? Or, what would I use to replace the text on the webbrowser... the design mode = "On" already, so I know I can do it manually...)
And,
Will the changes to txtrng.text be passed to the outerHTML After I perform the replace(txtrng.text,strText,strReplace) function?  I didn't ask the question correctly above.  

Thanks...

:)
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8191389
MAJOR PROBLEM!!!

When following my procedure using:
TxtRng = WebBrowser1.Document.body.createTextRange
ALL Of the Graphics on the HTML Page are SHAVED Out of the HTML... This is NOT GOOD!
How Can I Keep the formatting of the page and Graphics, yet replace text on the webbrowser control?

Thanks!

:)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8191834
Don't use anything that could contains "HTML" as part of its name.
Also, you could select entire page, copy to clipboard and paste in Richtext if you like.
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8191934
Hi Richie,

k, here's the problem with that:
I am passing the html code (with the changes) back to the RichTextBox, NOT the actual page...
The RichTextBox name is rtfContent, and the code is fashioned where it has HTML in part of some of the variables, but not with anything that will conflict with a syntax of a procedure/method, etc...

If I was to select the entire page, copy to clipboard, and paste into the RichTextBox, It would defeat the purpose... I wouldn't have any changes made via my procedures of find and replace...

***
Is there a place that keeps the graphics and HTML information when you assign, set and use the 'txtrng As IHTMLTxtRange' variable?  That is where the formatting and graphics get lost.
***

I checked out the links that you posted for me in the other thread, however, they didn't have what I was looking for with this issue.  Also, two of the links were not what they said they were... one was a 404 not found and another was similar, but brought me to another page... Thanks, though... :)

Did post a message here so I can give ya your extra 50 points yet?
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20559946.html

Thanks...

:)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8197764
"...
If I was to select the entire page, copy to clipboard, and paste into the RichTextBox, It would defeat the purpose... I wouldn't have any changes made via my procedures of find and replace...
..."

I hadn't that problem!

regarding broken links, just tell me which they are and i could send to you by mail.
cheers
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8198052
Hi Richie,

First, You haven't posted a message here:
http://search.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20559946.html
Do ya want the points, or not? ... :)

----------------------------
Re: The Links:
I get to here: http://msdn.microsoft.com/404/default.asp
From Here:
http://msdn.microsoft.com/library/periodic/period98/dom.htm
And I get this
VB Web has now moved to www.vbweb.co.uk.
from Here:
http://www.ccrowley.f9.co.uk/vbweb/controls/web_browser.htm

Thanks... :)

--------------------------------------------------------
Re: Copy and Paste Procedure:
Everything has been automated... As SOON As I make the First Change (Replacement of Text,) The HTML formatting is instantly stripped, including the graphics... Even if I was to copy and paste, the changes of a refreshed browser would have No Graphics, No Formatting, thereby will not work... If the browser is not refreshed, then the changes would not be visible, am I correct?
Also, the browser form is only loaded while the search and replace function is taking place.  The user does not manually change the text, it is all done via code... there is a Progress bar that displays the progress of the procedure, then closes the browser and passes the HTML with changes to the RichTextBox containing the HTML Source Code... the RichTextBox contains ONLY HTML Source Code, It does not Display actual page...

Is there another way around this issue?  I don't know if I like the idea of copying to the clipboard and pasting it into the RichTextBox, also, because it seems a bit of an unstable way of passing data from one place to the next, unless you were manually doing this.  It almost sounds like the sendkeys function, of which is quite unstable, in my opinion...

However, How would you perform the procedure and I'll test out the code in my project for a temporary work around.  I still don't think it will make a difference, though... Logically, I don't see how it could display the changes made without refreshing, let along be stable enough for the procedure.

Thanks...

:)


0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 240 total points
ID: 8198269
i don't think you can access the "changed" HTML by refreshing.
You see, we are changing what we "see", not what is in original HTML source code.
I think if we use .pastehtml maybe we could access the HTML "changed" but i didn't test it so i cannot say so.

Well i tested at we get "changed" html without refresh.
I did the following navigating to this very page with a webbrowser control:

Option Explicit
Dim iedoc As HTMLDocument
Private Sub Command1_Click()

 With iedoc
     Dim TxtRng As IHTMLTxtRange
     Dim sel As IHTMLTxtRange
     Set TxtRng = .body.createTextRange
     Do While TxtRng.findText("Comment") = True
         TxtRng.Select
         Set sel = iedoc.selection.createRange
         'sel.Text = "pings"
       sel.pasteHTML "<font color=green>Ping</font>"
      '   MsgBox "Text not found", vbInformation, App.EXEName
      Loop
 End With
Dim i As Integer
i = FreeFile
Open "c:\mytest.htm" For Output As #i
    Print #i, iedoc.documentElement.innerHTML
Close #i
End Sub

Private Sub Form_Load()
WB1.Navigate "http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20559954.html"

End Sub

Private Sub WB1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is WB1.Object) Then
    Set iedoc = WB1.Document
    Command1.Enabled = True
   
End If
End Sub

Do a new project, just as a test and see...
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8198783
Hi Richie,

I created a new project and ran it with your code.  I opened the page on load event, clicked the command button and it showed the green text.
Here's the problem.  I will not be navigating to a page.  I will be opening a page via HTML Source Code in the RichTextBox control and then making the changes then passing the HTML Source Code (after changes have been made) back to the RichTextBox control.

How could I use this code with my criteria listed above?
Also,
Set sel = iedoc.selection.createRange
sel.pasteHTML "<font color=green>Ping</font>"
This code is only pasting HTML, not changing text...

How could I Find then Replace text found using YOUR Code, then displaying the html changes in the RichTextBox control?

Thanks...

:)

-------
50 bonus points for Richie (from the other day) Here:
http://search.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20559946.html
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8200051
Hey Richie,

Please do me a favor,
post a message Here:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20561121.html
And:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20559946.html

I have resolved this issue and feel that you deserve the points...

I'll show the code that you gave me of which I modified to my needs below:

With iedoc
    Dim TxtRng As IHTMLTxtRange
    Dim sel As IHTMLTxtRange
    Set TxtRng = .body.createTextRange
    Do While TxtRng.findText(strFind) = True
        TxtRng.Select
        Set sel = iedoc.selection.createRange
        sel.pasteHTML strReplace ' Variable that replaces select text found...
     Loop
End With

Dim i As Integer
i = FreeFile
Open App.Path & "\FindReplace.html" For Output As #i
   Print #i, iedoc.documentElement.innerHTML
Close #i

THANK YOU VERY VERY VERY MUCH!!! ... :)
And PLEASE PLEASE PLEASE, Post a message on the two links of this comment so that I can give YOU the points!!!

:)
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8200055
Thanks, Richie...

You're Awesome!

:)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8200188
Well, as i told before, we were not understand each other.
glad to Help and thanks for "A" grade.
Now, i can post a comment on the others q., not before :D
0

Featured Post

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.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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