Link to home
Start Free TrialLog in
Avatar of AJAY CHADHA
AJAY CHADHAFlag for India

asked on

Highlight text on a webpage

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
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Hi, with automation it could be possible. How do you manage that web page?
Avatar of AJAY CHADHA

ASKER

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..??
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
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 "https://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


Ok Guys, thanks

I'll give it a try..!!

Ajay Chadha
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
oops, i forgot to add the HTML object library. I'll just test it now
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..?
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.
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Richie,

You did it man..!!

Thanks a lot.!

Ajay Chadha
Yeah, sometimes coin falls in the right place.
Thanks for "A" grade.
By the way, how is your site?
Hello Richie,

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

Ajay Chadha
Hello Richie,

Is there any way to change the back color of the text instead of changing the forecolor..?
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.
end user has the option to load "ANY" page
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?
Richie, Can you please elaborate on this issue a little more. I am not getting to you properly on this point.
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.
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
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.
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 "https://www.experts-exchange.com/questions/20297499/Highlight-text-on-a-webpage.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

Hello Richie,

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

Good Luck,

Ajay Chadha :)
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
Thanks Buddy!

Here comes your points :)

https://www.experts-exchange.com/questions/20735459/Points-for-Richie.html

Good Luck,

Ajay Chadha
Hello Richie,

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

Good Luck,

Ajay Chadha :)