|
-
Feb 22nd, 2007, 03:30 AM
#1
Thread Starter
Lively Member
[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]
-
Feb 22nd, 2007, 04:06 AM
#2
Re: Highlighting Text in a Web Browser Control
Welcome to VBForums ! 
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.
Last edited by iPrank; Feb 22nd, 2007 at 06:24 AM.
-
Feb 22nd, 2007, 06:06 AM
#3
Thread Starter
Lively Member
Re: Highlighting Text in a Web Browser Control
Thanxxx iPrank....
It is working great........
-
Feb 22nd, 2007, 06:12 AM
#4
Thread Starter
Lively Member
Re: Highlighting Text in a Web Browser Control
One more Issue iPrank.. how to clear the Highlighting of selection....
-
Feb 22nd, 2007, 06:28 AM
#5
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.
-
Feb 22nd, 2007, 06:35 AM
#6
Thread Starter
Lively Member
Re: Highlighting Text in a Web Browser Control
Thaxxx iPrank,,
i was strugling to get this for many days....
-
Feb 22nd, 2007, 06:37 AM
#7
Re: Highlighting Text in a Web Browser Control
You are welcome.
-
Feb 22nd, 2007, 10:20 PM
#8
Thread Starter
Lively Member
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.
-
Feb 22nd, 2007, 10:41 PM
#9
Hyperactive Member
Re: Highlighting Text in a Web Browser Control
 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
-
Feb 22nd, 2007, 10:43 PM
#10
Thread Starter
Lively Member
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
-
Feb 22nd, 2007, 10:57 PM
#11
Hyperactive Member
Re: Highlighting Text in a Web Browser Control
 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
-
Feb 22nd, 2007, 11:08 PM
#12
Thread Starter
Lively Member
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....
-
Feb 22nd, 2007, 11:13 PM
#13
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
Last edited by iPrank; Feb 22nd, 2007 at 11:17 PM.
-
Feb 22nd, 2007, 11:14 PM
#14
Hyperactive Member
Re: Highlighting Text in a Web Browser Control
 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?
Rate Me! Rate Me! Rate Me!
Time to fly.
Copyright GraysonSoft Inc. 2007
-
Feb 22nd, 2007, 11:16 PM
#15
Thread Starter
Lively Member
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
-
Feb 22nd, 2007, 11:16 PM
#16
Hyperactive Member
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
-
Feb 23rd, 2007, 12:02 AM
#17
Thread Starter
Lively Member
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....
-
Oct 17th, 2013, 02:36 PM
#18
New Member
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!
-
Oct 17th, 2013, 02:45 PM
#19
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.
-
Oct 17th, 2013, 06:54 PM
#20
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.
-
Oct 17th, 2013, 06:59 PM
#21
Re: Highlighting Text in a Web Browser Control
 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
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.
-
Oct 18th, 2013, 11:34 AM
#22
New Member
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?****
-
Oct 18th, 2013, 03:09 PM
#23
Re: [RESOLVED] Highlighting Text in a Web Browser Control
 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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|