Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Highlight text on a webpage

Posted on 2002-05-06
26
Medium Priority
?
714 Views
Last Modified: 2013-12-16
Does anyone knows how to highlight some text in a webpage. Like to make the background of all the occurances of word "hello" on the webpage to say red color.

Ajay Chadha
0
Comment
Question by:AJAY CHADHA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 11
26 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6991512
Hi, with automation it could be possible. How do you manage that web page?
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 6992869
Basiclaly, I am using Internet Explorer Object

like

Dim IE as InternetExplorer

So, I think, something related to change in IE.Document

What do you say..??
0
 
LVL 20

Expert Comment

by:hes
ID: 6994570
Try something like this:

Option Explicit
Dim WithEvents IE As InternetExplorer
Dim Complete As Boolean

Private Sub Form_Load()
Dim sHtml As String
Dim ff As Integer
Complete = False
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate2 "www.allapi.net"
Do While Complete = False
  DoEvents
Loop
sHtml = IE.Document.body.innerHTML

sHtml = Replace(sHtml, "the", "<font color=red>the</font>")
ff = FreeFile()
Open "C:\temp\doc.html" For Output As #ff
Print #ff, sHtml
Close #ff

DoEvents
   
IE.Navigate2 "C:\temp\doc.html "




End Sub
Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)

  Complete = True

End Sub
0
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6995163
Sorry, this is the closest i can reach, maybe you could work from it:

Option Explicit

Dim WithEvents IE As InternetExplorer
Private Sub Command1_Click()
Dim ieDoc As HTMLDocument
   Set ieDoc = IE.document
   With ieDoc
       Dim TxtRng As IHTMLTxtRange
       Dim sel As IHTMLTxtRange
       Set TxtRng = .body.createTextRange
       If TxtRng.findText(Text1.Text) = True Then
           TxtRng.Select
           Set sel = ieDoc.selection.createRange
           sel.Text = "pings"
       Else
           MsgBox "Text not found", vbInformation, App.EXEName
       End If
   End With
End Sub

Private Sub Form_Load()
Set IE = New InternetExplorer
With IE
    .navigate "http://www.experts-exchange.com/visualbasic/"
    .Visible = True
End With
End Sub


Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
   Command1.Enabled = True
Else
   Command1.Enabled = False
End If
End Sub


0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 6997105
Ok Guys, thanks

I'll give it a try..!!

Ajay Chadha
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 6997542
Hello Richie,

By running your code, VB gave error at Dim IE as HTMLDocument that the type is not defined and the same error at Dim TxtRng As IHTMLTxtRange
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 6997558
oops, i forgot to add the HTML object library. I'll just test it now
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 6997568
Hello Richie,

so far your code is the best one but it selects only first occurance. Is there a way to highlight all occurances of a particluar word..?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6997606
Sorry, my mistake.
i hadn't time to try it but i think is possible.
Unfortunatelly, i don't know how select all instances at same time. For each one, maybe with a loop until...
Some like:

do while TxtRng.findText(Text1.Text) = True
loop

The major problem is how turn selected text to, say, red.
The object hasn't style member or some like that that we could use.
Let me know.
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 400 total points
ID: 6997988
I got it!

Private Sub Command1_Click()
Dim ieDoc As HTMLDocument
  Set ieDoc = IE.document
  With ieDoc
      Dim TxtRng As IHTMLTxtRange
      Dim sel As IHTMLTxtRange
      Set TxtRng = .body.createTextRange
      Do While TxtRng.findText(Text1.Text) = True
          TxtRng.Select
          Set sel = ieDoc.selection.createRange
          'sel.Text = "pings"
        sel.pasteHTML "<font color=green>" & sel.Text & "</font>"
       '   MsgBox "Text not found", vbInformation, App.EXEName
       Loop
  End With
End Sub
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 6998098
Thanks Richie,

You did it man..!!

Thanks a lot.!

Ajay Chadha
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6998653
Yeah, sometimes coin falls in the right place.
Thanks for "A" grade.
By the way, how is your site?
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 7003537
Hello Richie,

Is there any way to change the back color of the text instead of changing the forecolor..?

Ajay Chadha
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 7711786
Hello Richie,

Is there any way to change the back color of the text instead of changing the forecolor..?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7717132
hum...
I didn't see your previous comment.
I doubt. there is no property of textrange that we could use to do that.
A question: Those pages belongs to you?. I mean, did you create it or end user has the option to load "ANY" page?
Let me know ASAP.
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 7730399
end user has the option to load "ANY" page
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7732149
Well, i am not sure but it is an idea:
We could use a document.write method to link that page to a stylesheet previously created and apply a class style from it in the selected text.
do you know what i mean?
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 7752950
Richie, Can you please elaborate on this issue a little more. I am not getting to you properly on this point.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7757641
did you use style sheets before,didn't you?
I saw your site a time ago and was thinking that your html knowledge would be helping on this.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8191744
Chadha, maybe it will not be useful to you now but i didn't forget the problem, so:

Private Sub Command1_Click()
 Set ieDoc = IE.document
 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.execCommand "backcolor", False, "blue"
      Loop
 End With
End Sub
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 9294233
Hello Richie,

How are you my friend ?

I need some more help reharding the Document Object. IS there an easy way to get all the links of a webpage using something like

for all links in IE.document

list1.additem link

next

and what is the most suitable way to click a link on a webpage programmticaly thru VB code. The links will be from ie.document

Looking to hear from you with sample code. I'll post points for you for this help.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9295750
Hi, there is actually a Links collection in the IE object model so if you use:

Dim WithEvents IE As InternetExplorer
Sub test()
Set IE = New InternetExplorer
With IE
    .navigate "http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20297499.html#9294233"
    .Visible = True
End With
End Sub

Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
    Dim lnk 'As IHTMLLinkElement
    For Each lnk In IE.document.links
        Debug.Print lnk.href
    Next
End If
End Sub

0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 9307432
Hello Richie,

Your help is much appreciated. Is there any way to click on a particular link programatically ?

Good Luck,

Ajay Chadha :)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9308971
Yes,
Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
   Dim lnk 'As IHTMLLinkElement
   For Each lnk In IE.document.links
       Debug.Print lnk.href
       if instr(1,lnk.href,"experts-exchange",vbtextcompare) then ' or whatever you would check
                lnk.click
                exit for   ' or whatever you need
       end if
   Next
End If
End Sub
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 9334901
Thanks Buddy!

Here comes your points :)

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20735459.html

Good Luck,

Ajay Chadha
0
 
LVL 8

Author Comment

by:AJAY CHADHA
ID: 9419904
Hello Richie,

Your points are still waiting for you on the link above.

Good Luck,

Ajay Chadha :)
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question