how would I do a text replacement loop?

C

cherold

I have a little macro to replace certain HTML tags in a document to a
special format. Since I don't know VB, I just recording a macro and
then cut and paste the code with different parameters, and when I
discover something new I need to replace I do another cut and paste.
But the ex-programmer in me hates such clunky code, so I was hoping
someone could tell me how to create a text replacement look, in which
I could create some sort of array of pairs like
( ("[A HREF=", "[LINK]") ("<P>", "[P"]) ("</P>", "[/P]) ("<BR>",
""))

and then loop through that array, putting the snippet of code I've
been using inside a loop that goes through each pair of the array.

This is the code I have below. As you can see, I just need to change
the .Text and .Replacement.Text variable for each replacement. Thanks
for any help.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<p><br>^p</p>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
J

Jacob Skaria

Hi

Use the below function to replace text. Specify the string to be replaced in
strSPChrs with a comma delimiter. Get the word contents to a variable; say
strData.

strData = Trim(ActiveDocument.Range)
strData = funCleanData(strData)

If this post helps click Yes
--------------
Jacob Skaria

Function funCleanData(strTemp) As String
Dim intTemp
Dim arrTemp
Dim strSPChrs As String
strSPChrs = "<B>,</B>,<U>,</U>,<I>,</I>"
arrTemp = Split(strSPChrs, ",")
funCleanData = strTemp
For intTemp = 0 To UBound(arrTemp)
funCleanData = Replace(UCase(funCleanData), arrTemp(intTemp), "")
Next
End Function
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use

Dim oRng As Word.Range
Dim FindList() As String

Dim ReplList() as String
Dim i As Long
FindList() = Split("[A HREF|<P>|</P>|<BR>", "|")
ReplList() = Split("[LINK]|[P"]|[/P]|", "|")
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For i = 0 To UBound(FindList)
With oRng.Find
.Text = FindList(i)
.Replacement.Text = ReplList(i)
.Forward = True
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
C

cherold

Awesome, thanks. (Sorry it took me so long to reply; my PC went
kablooie the day after I posted my question, so I haven't been able to
try this until now.)
 

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