Friday, October 5, 2012

VBA String Manipulations - Remove HTML tags

Sub RemoveTags()
' ______________________________________
'
' This macro removes tags such as < and >
' from a web page source code
' It works in Word
' ______________________________________
'

Dim MyRange As Range
Dim pos As Long

Set MyRange = ActiveDocument.Range
 With MyRange.Find
  Do While .Execute(findText:="(\<*\>)", _
   MatchWildcards:=True, _
   Wrap:=wdFindStop, Forward:=True) = True
   MyRange.Delete
  Loop
 End With

Set MyRange = Nothing

MsgBox "end macro"

End Sub


'______________________________________


'Another way to do the same job of removing html tags from the source code of a web page is:

'______________________________________



Sub Remove_All_Tags()
'______________________________________
'
' This VBA macro removes all tags from
' the source code of a web page
' For example, "<b>This text</b>"
' becomes "This text"
' This quick macro works in Word
'______________________________________
'
ActiveDocument.Select
If Selection.Find.Execute("<", 0, 0) Or Selection.Find.Execute("</", 0, 0) = True Then

Do

Selection.Extend (">")
Selection.Delete

ActiveDocument.Select

Loop Until Selection.Find.Execute("<", 0, 0) = False

Else
MsgBox "No < > tag was found"
End If

End Sub

No comments:

Post a Comment

You may comment or show me other VBA tricks, but don't rest assured I'll always reply because I only have 24 hours in a day's hard work, and only a few minutes a week to update this blog... I'll try my best though...