search and replace in VBA seems a little strange

P

Peter

I am planing to use vba to massage some HTML files to get rid of certain tags
and attributes. Below is the start of the VBA. All seems to be OK until I
start to use WILDCARD characters in the search and replace expressions and
then things SOMETIMES work and sometimes don't. e.g. the bit below should
look for a "<TABLE" tag and get rid of all the attributes apart from "BORDER="

Can someone tell me what I am doing wrong?

Sub ReplaceText()
Dim numbofReplacements As Integer
numofreplaces = 3
Dim FindStringArray(100) As String
Dim ReplaceStringArray(100) As String
Dim i As Integer

' Initialize the arrays
' with the find and replace strings

FindStringArray(0) = "find 0"
ReplaceStringArray(0) = "replace 0"
FindStringArray(1) = "find 1"
ReplaceStringArray(1) = "replace 1"
FindStringArray(2) = "\<table * border"
ReplaceStringArray(2) = "<table border" ' keep border tag in table
FindStringArray(3) = "border*\>"
ReplaceStringArray(3) = "border>"

For i = 0 To numofreplaces
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Wrap = wdFindContinue
With Selection.Find
.Text = FindStringArray(i)
.Replacement.Text = ReplaceStringArray(i)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Next 'i
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top