Underlining problem

L

LadyDungeness

I use Word XP. I want to find an UNDERLINED and CAPITALIZED " P. " and replace it with " P " without underlining. Then
I want to clear out the underlining on the numbers that precede and follow the "P" (This relates to legal citations, in case
anybody is interested.

Here is the macro. It worked when I recorded it, but I'm afraid to run it on another document, because I cannot see where it
specifies UNDERLINING.

Can somebody help me?


Lady Dungeness
Out of Danger until September
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~


-------------------------
Sub Macro10()
'
' Macro10 Macro
' Macro recorded 5/14/2008 by Normal
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"Default Paragraph Font")
With Selection.Find
.Text = " P. "
.Replacement.Text = " P "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
---------------------------------
 
D

Doug Robbins - Word MVP

Don't be afraid.

Save the document before you run it. If it doesn't do what you want, close
the document without saving it and then open it again. Or, use Undo.

--
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
 
J

Jay Freedman

You're correct that there's nothing about underlining in the recorded macro.
That's because the macroi recorder in Word 97 through 2003 has a bug
(finally fixed in Word 2007) that fails to record any kind of formatting in
a Find operation, although it does record ".Format = True". Read the "Fixing
broken Replace macros" section of
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm. The fix for
that is to insert the line

.Font.Underline = wdUnderlineSingle

in the list of things under "With Selection.Find".

However, that won't entirely solve the problem, because it can't remove the
underlining from the numbers before and after the "P" (since you never
"find" the numbers, only the "P"). The complete fix is to use a wildcard
search (http://www.gmayor.com/replace_using_wildcards.htm) instead of a
regular search. To record this, click the More button in the Find dialog and
check the "Use wildcards" box. In the Find What box, paste in

([0-9]{1,}) P. ([0-9]{1,})

In the Replace With box, paste in

\1 P \2

and click Replace All. You'll still have to go into the recorded macro and
add the .Font.Underline statement.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
L

LadyDungeness

This is very helpful. It explains why some of my Macro efforts weren't working -- not all mty fault! I'll fiddle with the
underlining and the wildcards.


Lady Dungeness
Out of Danger until September
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
 
T

Twayne

....
I'm afraid to
run it on another document, because I cannot see where it specifies
UNDERLINING.

Can somebody help me?

I believe clearformatting removes the underline and everything else
format-wise. So if there is other formatting (not capitalization, etc.,
but literally formating) it'll also remove those of course. Haven't
scripted in really long time but I think if you check the clear
formatting commands in Help it'll explain that.
I dont' think it'll be an issue with citations unless maybe it messes
up the line numbers, so check those too; you might need to except them
somehow.

As for testing a macro/vba/etc, always copy a document, or a portion of
the document if it's a large one, to another similar filename first.
e.g. If it's lawn.odt, make the other file lawn-testing.odt or
lawn_macro.odt; something like that.
By keeping the name and just adding to the end of it it, you also
keep it near its original in directory/folder listings.
Then you can hack/play all you want without worrying about damaging
the original.
When everything works, just transfer the macro to a full copy of the
original, and if that works, go ahead with whatever you'd like to do
with all of them.

Beware; September approacheth < G >

Regards,

Twayne
 
J

Jay Freedman

Hi Twayne,
I believe clearformatting removes the underline and everything else
format-wise. So if there is other formatting (not capitalization, etc.,
but literally formating) it'll also remove those of course.

No, it doesn't work that way.

The .Find.ClearFormatting doesn't have any effect at all on the text in the
document. It affects only the set of formatting settings that the .Find object
looks for during the search. It's equivalent to putting the cursor in the Find
What box of the Replace dialog and clicking the "No Formatting" button (at the
bottom of the dialog after the More button is clicked).

The .Find.Replacement.ClearFormatting removes any formatting settings that might
be left over from a previous replacement. It's equivalent to putting the cursor
in the Replace With box of the Replace dialog and clicking the "No Formatting"
button. If you find bold text and replace with text that has no formatting
settings, it will stay bold -- to change the text's format, you'd have to mark
the replacement as Not Bold (or .Find.Replacement.Bold = False in a macro).

Good practice in macro programming is to start every search or replacement by
doing ClearFormatting on both the .Find and the .Find.Replacement so you start
from a known base; and then specify the formatting for the search and for the
replacement explicitly.

Your advice about testing on a copy of the document and not on the original is
spot on.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 

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