• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

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

:)
0
FirstBorn
Asked:
FirstBorn
  • 10
  • 7
1 Solution
 
Richie_SimonettiIT OperationsCommented:
Hi, see if there is no frames in the document. The count should be the same, not diferent.
0
 
FirstBornAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
FirstBornAuthor Commented:
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
 
FirstBornAuthor Commented:
0
 
Richie_SimonettiIT OperationsCommented:
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
 
FirstBornAuthor Commented:
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
 
FirstBornAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
FirstBornAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
"...
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
 
FirstBornAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
FirstBornAuthor Commented:
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
 
FirstBornAuthor Commented:
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
 
FirstBornAuthor Commented:
Thanks, Richie...

You're Awesome!

:)
0
 
Richie_SimonettiIT OperationsCommented:
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

Technology Partners: 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!

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now