[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?
Re: Highlighting Text in a Web Browser Control
Welcome to VBForums ! :wave:
try this:
VB Code:
' Add a reference to Microsoft HTML Object Library
Option Explicit
Dim strOriginalHTML As String
Private Sub FindAndHighlight(strText As String)
Dim doc As HTMLDocument
Dim tr As IHTMLTxtRange
Set doc = WebBrowser1.document
Set tr = doc.body.createTextRange
strOriginalHTML = doc.body.innerHTML 'backup for undo
Do While tr.findText(strText, 1, 0) 'while we have result
'Set the highlight, now background color will be Lime -->
tr.pasteHTML "<span style=" & """" & "background-color: Lime; font-weight: bolder;" & """" & ">'" & _
tr.htmlText & "'</span>'"
'
'When we find a match, we ask to scroll the window -->
tr.scrollIntoView True
Loop
End Sub
Private Sub RemoveHighlight()
Dim doc As HTMLDocument
Set doc = WebBrowser1.document
doc.body.innerHTML = strOriginalHTML
End Sub
Private Sub Command1_Click()
FindAndHighlight "Visual Basic"
End Sub
Private Sub Command2_Click()
RemoveHighlight
End Sub
Private Sub Form_Load()
WebBrowser1.navigate "http://www.vbforums.com/"
End Sub
Converted from a Delphi code.
Re: Highlighting Text in a Web Browser Control
Thanxxx iPrank....
It is working great........
Re: Highlighting Text in a Web Browser Control
One more Issue iPrank.. how to clear the Highlighting of selection....
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.
Re: Highlighting Text in a Web Browser Control
Thaxxx iPrank,,
i was strugling to get this for many days....
Re: Highlighting Text in a Web Browser Control
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?? :confused:
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?? :confused:
Try this:
Code:
Original_Document = doc.body.innerHTML
I think doc.body.innerHTML is read only.
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
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?
:thumb:
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....
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:
' Add a reference to Microsoft HTML Object Library
Option Explicit
Dim strOriginalHTML As String
Private Sub FindAndHighlight(strText As String)
Dim doc As HTMLDocument
Dim tr As IHTMLTxtRange
Set doc = WebBrowser1.document
Set tr = doc.body.createTextRange
[b]strOriginalHTML = tr.htmlText 'backup for undo[/b]
Do While tr.findText(strText, 1, 0) 'while we have result
'Set the highlight, now background color will be Lime -->
tr.pasteHTML "<span style=" & """" & "background-color: Lime; font-weight: bolder;" & """" & ">'" & _
tr.htmlText & "'</span>'"
'
'When we find a match, we ask to scroll the window -->
tr.scrollIntoView True
Loop
End Sub
[b]Private Sub RemoveHighlight2()
Dim doc As HTMLDocument
Dim tr As IHTMLTxtRange
Set doc = WebBrowser1.document
Set tr = doc.body.createTextRange
'tr.Select '--> If this doesn't work, try uncommenting this line
tr.pasteHTML strOriginalHTML
End Sub[/b]
Private Sub Command1_Click()
FindAndHighlight "his"
End Sub
Private Sub Command2_Click()
RemoveHighlight2
End Sub
Private Sub Form_Load()
WebBrowser1.navigate "http://www.forbes.com/feeds/ap/2007/02/20/ap3446861.html"
End Sub
Re: Highlighting Text in a Web Browser Control
Quote:
Originally Posted by yadavrahul143
I clicked on the link and it temporarily redirected me to their welcome page.
Have you highlighted any text on this page?
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
Re: Highlighting Text in a Web Browser Control
The page I clicked generated errors.
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....
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!
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. :)
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
Re: Highlighting Text in a Web Browser Control
Quote:
Originally Posted by
ma12
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
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?****
Re: [RESOLVED] Highlighting Text in a Web Browser Control
Quote:
Originally Posted by
ma12
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.