Hi Peter,
You're going about this completely the wrong way. Switch over to using a
wildcard search (
http://word.mvps.org/faqs/general/UsingWildcards.htm) for
the entire phrase from the start tag all the way to the end tag, and then
you can use the wdReplaceAll parameter in the .Execute to process the
entire
document in one shot. This will be orders of magnitude faster than
crawling
through the document the way you're doing it now.
This sample macro shows a number of features: How to use a range object
for
Replace; how to use a subroutine with an argument to change its behavior;
how to use Select Case to do different actions based on the value of the
argument; and how to use a wildcard expression to match HTML tags (more on
this subject below the macro).
Private Sub ReplaceFormattingTags(strTag As String)
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.MatchWildcards = True
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.MatchCase = False
.Text = "\<" & strTag & "\>([!\<\>]@)\</" & strTag & "\>"
.Replacement.Text = "\1"
Select Case strTag
Case "i", "I"
.Replacement.Font.Italic = True
.Execute Replace:=wdReplaceAll
Case "b", "B"
.Replacement.Font.Bold = True
.Execute Replace:=wdReplaceAll
Case "u", "U"
.Replacement.Font.Underline = wdUnderlineSingle
.Execute Replace:=wdReplaceAll
' add more cases for tags you want to handle
Case Else
' do nothing
End Select
End With
End Sub
Public Sub test()
ReplaceFormattingTags ("i")
ReplaceFormattingTags ("b")
End Sub
The wildcard expression in the macro is constructed like this:
Depending on the value of the argument strTag, the first portion "\<" &
strTag & "\> will match an opening tag like <i> or <I> (it will match
either
because of the .MatchCase = False setting). As explained in the Wildcards
article, the backslashes tell VBA to treat the < and > as actual
characters
to be matched, rather than their special wildcard meanings (start and end
of
words).
Similarly, the ending portion \</" & strTag & "\>" will match the
corresponding closing tag like </i> or </I>.
The portion ([!\<\>]@) will match any sequence of characters that does
*not*
contain any < or > character. The ( ) tell VBA that whatever is matched by
this portion will be given the alias \1 for use in the .Replacement.Text.
The effect of this is to remove the tags while simultaneously applying the
specified formatting to the text between the tags.
--
Regards,
Jay Freedman
Microsoft Word MVP
Hi,
I'm trying to write a macro to convert tags (like html tags) using
the Range object.
I can do it using the selection object easy enough but would like to
convert to using a range object for performance.
Here is a the selection object code.
Selection.StartOf (6) ' Convert Bold tags
Selection.Find.Text = "<B>"
Selection.Find.Execute
Do While Selection.Find.Found
Selection.ExtendMode = True
Selection.Find.Text = "<P>" ' end of bold
Selection.Find.Execute
Selection.Font.Bold = True
Selection.MoveRight 1, 1
Selection.ExtendMode = False
Selection.Find.Text = "<B>"
Selection.Find.Execute
Loop
Any help much appreciated.
Thanks Peter