PDA

View Full Version : [SOLVED:] Find/replace formatting problem (bold spills over from previous line)



Helen269
02-27-2019, 03:58 AM
I have situations in a Word doc where I have this:

Name
[unclear]

I have a macro that looks for paragraph marks, the backwards capital P, and changes the '[u' to '[U'. The only problem is it then becomes:

Name
[Unclear]

How do I stop the bold formatting on the '[U' bit?

This is the VBA:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p[u"
.Replacement.Text = "^p[U"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Many thanks,
Helen

gmaxey
02-27-2019, 08:32 AM
That is because you are replacing the found range with the formatted paragraph mark preceding the text. Try:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "^p[u"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
While .Execute
oRng.Characters.Last = "U"
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub

Helen269
02-27-2019, 11:26 AM
Sorry, Greg, for some reason that didn't work.
As it will be part of collection of subs in one macro to do different things to the document in one go then an easy workaround might be to have the last sub go through the document, find all instances of '[U' and just make them not bold. I'll see what I can find out there to do this. Thanks.

Helen269
02-27-2019, 11:36 AM
Aaaaand adapting a recorded macro works. Thanks, Greg. Case closed. :-)

gmaxey
02-27-2019, 12:06 PM
Good. Puzzled that it didn't work though. Worked here.