No italic in my Search/Replace VBA?

S

Souriane

I have the following macro bellow which does the following:

Searches for the following: (district ^#))i).
And replaces it by: )i).

In the document, the text right before “(district ^#))i)” is in italic
but not “(district ^#))i)”.
Once I have done the Search/Replace, “)i)” become in italic also.
How can I have the code modify so that the “)i)” is not in italic
after the search replace?

Thank you!
S.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorGreen
‘ my code “)i)” is in green in the text and I want it to stay that way
after the Search/Replace

With Selection.Find
.Text = " (district ^#))i)."
.Replacement.Text = ")i)."
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
D

Doug Robbins - Word MVP

Try

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=" (district ^#))i).", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set myrange = Selection.Range
myrange.Text = Right(myrange.Text, 4)
myrange.Font.Italic = False
myrange.Font.Color = wdColorGreen
Loop
End With


--
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
I have the following macro bellow which does the following:

Searches for the following: (district ^#))i).
And replaces it by: )i).

In the document, the text right before “(district ^#))i)” is in italic
but not “(district ^#))i)”.
Once I have done the Search/Replace, “)i)” become in italic also.
How can I have the code modify so that the “)i)” is not in italic
after the search replace?

Thank you!
S.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Color = wdColorGreen
‘ my code “)i)” is in green in the text and I want it to stay that way
after the Search/Replace

With Selection.Find
.Text = " (district ^#))i)."
.Replacement.Text = ")i)."
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
P

Pesach Shelnitz

This problem can be resolved with less extensive changes to the original
macro without abandoning the Replace functionality and without introducing a
Range object as follows:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = " (district ^#))i)."
.Replacement.Text = ")i)."
.Forward = True
.Wrap = wdFindStop
Do While .Execute(Replace:=wdReplaceOne) = True
Selection.Font.Italic = False
Selection.Font.Color = wdColorGreen
Selection.Collapse Direction:=wdCollapseEnd
Loop
End With
 
K

Klaus Linke

Just to mention another "trick": A wildcard replacement would also have kept
the formatting.

Find what: \(district [0-9]\)(\)i\))
Replace with: \1

Regards,
Klaus
 

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