Removing a certain tag in an HTML String


I have a variable holding an HTML file content what I want is the following:

Remove all the <a> tags from it, what I want is to capture the link tag and remove it with its contents whatever it is an image , or a text or anything

Please help

Who is Participating?
Richie_SimonettiIT OperationsCommented:
One possible solution:
'Set a reference to microsoft Intenet controls
'Set a rteference to microspft html object library
' try this:

Option Explicit

Dim WithEvents IE As InternetExplorer
Dim WithEvents iedoc As HTMLDocument

Dim sContents As String
Private Sub Form_Load()
Set IE = New InternetExplorer
With IE
    .navigate ""
    .Visible = True
End With
End Sub

Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
    Set iedoc = IE.document
    Dim lnk As HTMLLinkElement
    For Each lnk In iedoc.links
        lnk.disabled = True
End If
End Sub
Ryan ChongCommented:
HTMLString = Replace$(HTMLString,"<a>","") ?
also this,
HTMLString = Replace$(HTMLString,"</a>","")

This will remove the hyperlinks from the htm page.

But as I understand from your code is you want to remove all the text within the <a> and </a> tags. If it is so, then find for <a> and </a> tags using mid or instr fns and replace that with vbnullstring(" ")

Hope this helps!


Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Richie_SimonettiIT OperationsCommented:
IMHO, you have two choices:
1) Second one by priya_pbk but changing the first to "<a" since there is not an "<A>" alone ;)
2) You could use HTMLDocument object.
marounkAuthor Commented:
where can I find example on the HTMLDocument Object ???

Richie, I tried your example, but gives me an error at this line , ie:
lnk.disabled = True

mentions "Object doesn't support this property or method"

I have put the references and wrote down the code, it opens the IE and then shows this error.

Was curious to know how the 'lnk.disabled=true' can actually disable links in a web page!! Am I missing something here.


Richie_SimonettiIT OperationsCommented:
I did a test before post it with no problems!
Maybe a MSHTML.dll version problem?

marounk, that code didn't solve your problem?, then, why did you accept my comment as answer?
marounkAuthor Commented:
After adding the Refrences in VB you code did work but it is not finding any Links in any Page !!!

I did close this thread because I was desperate !

Thnx for caring
Richie_SimonettiIT OperationsCommented:
Strange, i did a test with and everything worked fine.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.