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

asked on

Count Text in Webbrowser Control...

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...

:)
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Hi, see if there is no frames in the document. The count should be the same, not diferent.
Avatar of FirstBorn

ASKER

Hi Richie,

First, Did You get this?
http://search.experts-exchange.com/questions/20559946/Question-for-Richie-Simonetti.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.

:)

Take a look at this:
https://www.experts-exchange.com/questions/20297499/Highlight-text-on-a-webpage.html

By the way, did you see the links posted in the other Q?
Hi Richie,

Another Question at the very bottom...

The link that you have provided:
https://www.experts-exchange.com/questions/20297499/Highlight-text-on-a-webpage.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/questions/20559946/Question-for-Richie-Simonetti.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.

:)
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?

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...

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

:)
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.
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?
https://www.experts-exchange.com/questions/20559946/Question-for-Richie-Simonetti.html

Thanks...

:)
"...
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
Hi Richie,

First, You haven't posted a message here:
http://search.experts-exchange.com/questions/20559946/Question-for-Richie-Simonetti.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...

:)


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
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/questions/20559946/Question-for-Richie-Simonetti.html
Hey Richie,

Please do me a favor,
post a message Here:
https://www.experts-exchange.com/questions/20561121/Editing-text-on-Webbrowser-Control-Strips-Formatting-and-Graphics-on-HTML-Source-Code.html
And:
https://www.experts-exchange.com/questions/20559946/Question-for-Richie-Simonetti.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!!!

:)
Thanks, Richie...

You're Awesome!

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