Shimon,
With a little additional testing I found that using a one of the wildcard
symbols as the start or end flag could cause errors or loops unless
preceeded by a backslash \.
Take the example:
Now is @ the time for all good * men to come @ to the aid of thier *
country.
The @ is the start flag and the * is the end flag. As both are wildcard
characters, they should be put in the input box as \@ and \*
If we had:
Now is ~ the time for all good + men to come ~ to the aid of thier +
country.
Since niether the start flag ~ or the end flag + is a wildcard character,
they could be entered directly as ~ and +
If you don't want to chop the strings to exclude the start and end flags
then just delete the lines I have marked with ***
Sub ScratchMacro2()
Dim myRange As Range
Dim startFlag As String
Dim endFlag As String
Dim x As Integer ***
Dim y As Integer ***
Set myRange = ActiveDocument.Range
startFlagRetry:
startFlag = InputBox("Enter the start flag (e.g., aaaa)", "Start Flag")
If InStr("!@*(){}[]{?<>", startFlag) > 0 Then
MsgBox "Each wildcard symbol (!@*(){}]{?<>) used for flags" _
& " must be preceded by a backslash \", vbOKOnly
GoTo startFlagRetry
End If
x = Len(startFlag) ***
endFlagRetry:
endFlag = InputBox("Enter the end flag (e.g., zzzz)", "End Flag")
If InStr("!@*(){}[]{?<>", endFlag) > 0 Then
MsgBox "Each wildcard symbol (!@*(){}]{?<>) used for flags" _
& " must be preceded by a backslash \", vbOKOnly
GoTo endFlagRetry
End If
y = Len(endFlag) ***
With myRange.Find
.ClearFormatting
.MatchWildcards = True
.Text = startFlag & "*" & endFlag
While .Execute
myRange.Start = myRange.Start + x ***
myRange.End = myRange.End - y ***
With myRange
.Font.Color = wdColorGreen
End With
myRange.Collapse direction:=wdCollapseEnd
Wend
End With
End Sub