Results 1 to 23 of 23

Thread: [RESOLVED] Highlighting Text in a Web Browser Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Resolved [RESOLVED] Highlighting Text in a Web Browser Control

    I have a VB 6.0 windows app that includes a web browser control in which I display a variety of HTML pages that are a part of my project. I would like to include a Find and Find Next feature whereby the user can search the page being displayed for a string. If I find the search string then I would like to highlight it for the user (similar to the IE Find feature) ... my problem is that I don't know how to select (i.e. highlight) the found text in the browser control. Can someone enlighten me?
    Last edited by yadavrahul143; Feb 23rd, 2007 at 01:39 AM. Reason: [Resolved]

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Highlighting Text in a Web Browser Control

    Welcome to VBForums !
    try this:
    VB Code:
    1. ' Add a reference to Microsoft HTML Object Library
    2. Option Explicit
    3. Dim strOriginalHTML As String
    4.  
    5. Private Sub FindAndHighlight(strText As String)
    6.     Dim doc As HTMLDocument
    7.     Dim tr As IHTMLTxtRange
    8.     Set doc = WebBrowser1.document
    9.     Set tr = doc.body.createTextRange
    10.     strOriginalHTML = doc.body.innerHTML 'backup for undo
    11.  
    12.     Do While tr.findText(strText, 1, 0) 'while we have result
    13.         'Set the highlight, now background color will be Lime -->
    14.         tr.pasteHTML "<span style=" & """" & "background-color: Lime; font-weight: bolder;" & """" & ">'" & _
    15.            tr.htmlText & "'</span>'"
    16.         '
    17.         'When we find a match, we ask to scroll the window -->
    18.         tr.scrollIntoView True
    19.     Loop
    20.  
    21. End Sub
    22.  
    23. Private Sub RemoveHighlight()
    24.     Dim doc As HTMLDocument
    25.     Set doc = WebBrowser1.document
    26.     doc.body.innerHTML = strOriginalHTML
    27. End Sub
    28.  
    29. Private Sub Command1_Click()
    30.     FindAndHighlight "Visual Basic"
    31. End Sub
    32.  
    33. Private Sub Command2_Click()
    34.     RemoveHighlight
    35. End Sub
    36.  
    37. Private Sub Form_Load()
    38.     WebBrowser1.navigate "http://www.vbforums.com/"
    39. End Sub
    Converted from a Delphi code.
    Last edited by iPrank; Feb 22nd, 2007 at 06:24 AM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    Thanxxx iPrank....


    It is working great........

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    One more Issue iPrank.. how to clear the Highlighting of selection....

  5. #5
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Highlighting Text in a Web Browser Control

    I've edited above post, removed some unwanted codes and added a RemoveHighlight procedure. See if that helps.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    Thaxxx iPrank,,

    i was strugling to get this for many days....

  7. #7
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Highlighting Text in a Web Browser Control

    You are welcome.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    Hi iPrank,

    The above code worked but it is giving error for following URL

    http://www.forbes.com/feeds/ap/2007/...ap3446861.html

    while clearing the highlighting..

    "Run-time error '601':

    Application-defined or object-defined error"



    " doc.body.innerHTML = Original_Document"
    On this line

    will u please help me??
    Last edited by yadavrahul143; Feb 22nd, 2007 at 10:37 PM.

  9. #9
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Highlighting Text in a Web Browser Control

    Quote Originally Posted by yadavrahul143
    Hi iPrank,

    The above code worked but it is giving error for following URL

    http://www.forbes.com/feeds/ap/2007/...ap3446861.html

    while clearing the highlighting..

    "Run-time error '601':

    Application-defined or object-defined error"



    " doc.body.innerHTML = Original_Document"
    On this line

    will u please help me??
    Try this:

    Code:
    Original_Document = doc.body.innerHTML
    I think doc.body.innerHTML is read only.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    Hi tommygrayson,

    i am using code posted in 2nd post by iPrank... and this error is with the
    Private Sub RemoveHighlight() Subroutine....

    please look into the code posted above....

    Thanx

  11. #11
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Highlighting Text in a Web Browser Control

    Quote Originally Posted by yadavrahul143
    Hi tommygrayson,

    i am using code posted in 2nd post by iPrank... and this error is with the
    Private Sub RemoveHighlight() Subroutine....

    please look into the code posted above....

    Thanx
    I don't see any error on IPrank's code.

    Could you post your code?

    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    The Code posed by IPrank helped me very much but it is giving error for only one URL given below

    http://www.forbes.com/feeds/ap/2007.../ap3446861.html

    please try using this url for 2 to 3 searches and clear them simultaneously....

  13. #13
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Highlighting Text in a Web Browser Control

    Here is another way. See if this helps.
    BTW, my IE is saying, that page has script error. Maybe that is causing the problem.
    VB Code:
    1. ' Add a reference to Microsoft HTML Object Library
    2. Option Explicit
    3. Dim strOriginalHTML As String
    4.  
    5. Private Sub FindAndHighlight(strText As String)
    6.     Dim doc As HTMLDocument
    7.     Dim tr As IHTMLTxtRange
    8.     Set doc = WebBrowser1.document
    9.     Set tr = doc.body.createTextRange
    10.     [b]strOriginalHTML = tr.htmlText 'backup for undo[/b]
    11.  
    12.     Do While tr.findText(strText, 1, 0) 'while we have result
    13.         'Set the highlight, now background color will be Lime -->
    14.         tr.pasteHTML "<span style=" & """" & "background-color: Lime; font-weight: bolder;" & """" & ">'" & _
    15.            tr.htmlText & "'</span>'"
    16.         '
    17.         'When we find a match, we ask to scroll the window -->
    18.         tr.scrollIntoView True
    19.     Loop
    20.  
    21. End Sub
    22.  
    23. [b]Private Sub RemoveHighlight2()
    24.     Dim doc As HTMLDocument
    25.     Dim tr As IHTMLTxtRange
    26.     Set doc = WebBrowser1.document
    27.     Set tr = doc.body.createTextRange
    28.     'tr.Select '--> If this doesn't work, try uncommenting this line
    29.     tr.pasteHTML strOriginalHTML
    30. End Sub[/b]
    31.  
    32. Private Sub Command1_Click()
    33.     FindAndHighlight "his"
    34. End Sub
    35.  
    36. Private Sub Command2_Click()
    37.     RemoveHighlight2
    38. End Sub
    39.  
    40. Private Sub Form_Load()
    41.     WebBrowser1.navigate "http://www.forbes.com/feeds/ap/2007/02/20/ap3446861.html"
    42. End Sub
    Last edited by iPrank; Feb 22nd, 2007 at 11:17 PM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  14. #14
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Highlighting Text in a Web Browser Control

    Quote Originally Posted by yadavrahul143
    The Code posed by IPrank helped me very much but it is giving error for only one URL given below

    http://www.forbes.com/feeds/ap/2007.../ap3446861.html

    please try using this url for 2 to 3 searches and clear them simultaneously....
    I clicked on the link and it temporarily redirected me to their welcome page.

    Have you highlighted any text on this page?
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    No... when u go to the main page of the news use the highlight button for that page

  16. #16
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Highlighting Text in a Web Browser Control

    The page I clicked generated errors.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    96

    Re: Highlighting Text in a Web Browser Control

    Hi IPrank,

    By this change the code is working perfectly.... in case of anyother issues i will contact u....

  18. #18
    New Member
    Join Date
    Oct 2013
    Posts
    8

    Re: Highlighting Text in a Web Browser Control

    Hi iPrank,

    I like your solution to highlighting text in a WB!

    1. This is my first post on this forum. Possible I should start new thread, but I not sure.

    2. I have question to you. ***Is it possible to copy the content of one Web Browser control to another one in VB6?***

    Assume that WebBrowser1 is currently loaded (like WebBrowser1.Navigate "www.yahoo.com").

    Following to your example I tried

    Private Sub cmdClone_Click()
    Dim doc As HTMLDocument
    Dim tr As IHTMLTxtRange
    Dim doc1 As HTMLDocument
    Dim tr1 As IHTMLTxtRange
    Dim sSourceHtml As String


    WebBrowser2.Silent = True
    WebBrowser2.Navigate ("about:blank")
    Set doc = WebBrowser1.Document
    Set tr = doc.body.createTextRange
    DoEvents
    sSourceHtml = tr.htmlText
    DoEvents

    Set doc1 = WebBrowser2.Document
    Set tr1 = doc1.body.createTextRange
    DoEvents
    tr1.pasteHTML sSourceHtml
    DoEvents
    End Sub

    This procedure refreshes the second browser but instead or pictures it shows icons, it damages the page layout etc. What I am doing wrong? Can this cloning be done? Of course I can get the URL of first WB and use it to load second WB, but it works too slow. Or there is no way to clone the browser content without connedction to web server?

    Please advice.

    Thanks!

  19. #19
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Highlighting Text in a Web Browser Control

    Hi ma12 ! Welcome to the forums !

    Unfortunately, I've moved to DotNet a long time ago. I don't have VB6 installed on my computer, so can't be sure.

    But, my guess is webbrowser.document.execcommand should work.
    Please do a forum search or google search on those keywords.

    here are links to a few good WB tutorials. Those articles helped me a lot on different scenarios. I hope they will help you too.

    PS: You can always create new thread.
    Last edited by iPrank; Oct 17th, 2013 at 02:50 PM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  20. #20
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Highlighting Text in a Web Browser Control

    So should I assume this works only for text that is visible and not inner text of the HTML

    BTW how do you remove the single quotes around the highlighted text


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  21. #21
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Highlighting Text in a Web Browser Control

    Quote Originally Posted by ma12 View Post
    Is it possible to copy the content of one Web Browser control to another one in VB6?
    Can you not just get the text from WB1 using

    InnerHTML = ...Document.InnerHTML or something like that and then insert that into WB2 using

    WB2.write InnerHTML or something like that


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  22. #22
    New Member
    Join Date
    Oct 2013
    Posts
    8

    Re: [RESOLVED] Highlighting Text in a Web Browser Control

    Thank you for your suggestion, jmsrickland.

    I tried your idea to clone WB as
    Set doc = WebBrowser1.Document
    sSourceHtml = doc.body.innerHTML
    WebBrowser2.Document.write sSourceHtml
    but unfortunately second WB losts the layout, formattings and picture sizes.

    I also tried iPrank suggestion to use execCommand. I cloned WB as
    Set doc = WebBrowser1.Document
    Set tr = doc.body.createTextRange
    DoEvents
    l = tr.execCommand("COPY", False, Null) ' copy to clipboard '

    Set doc2 = WebBrowser2.Document
    Set tr2 = doc2.body.createTextRange
    l = tr2.execCommand("PASTE", False, Null) 'paste from clipboard

    but second WB also losts the layout, formattings and picture sizes.

    I post the both codes below.


    Private Sub cmdClone1_Click()
    Dim doc As HTMLDocument
    Dim tr As IHTMLTxtRange
    Dim doc2 As HTMLDocument
    Dim tr2 As IHTMLTxtRange
    Dim sSourceHtml As String
    Dim l As Long

    'WebBrowser1.Navigate ("www.Microsoft.com") 'was already completed on previous step

    WebBrowser2.Silent = True
    WebBrowser2.Navigate ("about:blank")
    Set doc = WebBrowser1.Document
    DoEvents
    sSourceHtml = doc.body.innerHTML
    WebBrowser2.Document.write sSourceHtml
    DoEvents
    End Sub

    Private Sub cmdClone2_Click()
    Dim doc As HTMLDocument
    Dim tr As IHTMLTxtRange
    Dim doc2 As HTMLDocument
    Dim tr2 As IHTMLTxtRange
    Dim sSourceHtml As String
    Dim l As Long

    'WebBrowser1.Navigate ("www.Microsoft.com") 'was already completed on previous step

    WebBrowser2.Silent = True
    WebBrowser2.Navigate ("about:blank")

    Set doc = WebBrowser1.Document
    Set tr = doc.body.createTextRange
    DoEvents
    l = tr.execCommand("COPY", False, Null) ' copy to clipboard '

    Set doc2 = WebBrowser2.Document
    Set tr2 = doc2.body.createTextRange
    l = tr2.execCommand("PASTE", False, Null) 'paste from clipboard
    End Sub

    ****ANY SUGGESTIONS?****

  23. #23
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Highlighting Text in a Web Browser Control

    Quote Originally Posted by ma12 View Post
    Thank you for your suggestion, jmsrickland.

    I tried your idea to clone WB as

    Set doc = WebBrowser1.Document
    sSourceHtml = doc.body.innerHTML
    WebBrowser2.Document.write sSourceHtml

    but unfortunately second WB losts the layout, formattings and picture sizes.
    That's what I was in fear of. Here's why. When you navigate to a website IE resolves all URLs (on Load as a result of Navigate) so it knows where to get things but when you copy the HTML from WebBrowser1 over to WebBrowser2 IE does not Load that page; it doesn't go through the Loading process which results with no links resolved, so IE simply displays the page as is (I think it actually executes the HTML). I didn't think of that when I made my suggestion.

    I have used the .write in some of my projects but only to execute certain things like Java scripts, etc, but never to load an image, for example, from a remote web site unless I supplied the full URL

    Anyway since you asked if it's possible to copy the content of one Web Browser control to another one then why wouldn't you just navigate to the same web site with WebBrowser2. I don't understand why the copy since navigating there you wind up with the same HTML but it will have the URLs resolved.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width