Find and replace followup

G

Guest

Hi everybody,
I got my macro recorded so I can replace my text, but for some reason, when
I run it, it does nothing. I know Microsoft Word has some weird bugs in it
that doesn't always show paragraph and font formatting when you replace, so I
tried to fix it and it doesn't look like I was sucessful. Anyway, can anyone
tell me where I need to fix this macro?

Thanks again!
Jezzica85

Sub Tagger()
' Replace centered, otherwise unformatted text with itself, surrounded by
[C] tags, bolded
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[C]^&[/C]"
.Replacement.Font.Bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace centered italicized text with itself, surrounded by [CI] tags
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[CI]^&[/CI]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace left aligned italicized text with itself, surrounded by [LI] tags
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[LI]^&[/LI]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace left aligned, otherwise unformatted text with itself, surrounded
by [L] tags
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[L]^&[/L]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
G

Graham Mayor

It helps if you stick to the same thread for the same problem!
Your macro does nothing because it searches for nothing and replaces with
something. The macro recorder is not capable of recording all actions and
recording formatting is one of the things it cannot do. An example of a
macro to find formatted text (and replace witha text string) is shown below.
I have inserted the alternative lines so you can experiment. Note that the
tags may not go where you might expect.

Sub ReplaceExample()

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
'**********************
.Text = ""
.Font.Italic = True
'.Font.Italic = False
.ParagraphFormat.Alignment = wdAlignParagraphLeft
'.ParagraphFormat.Alignment=wdAlignParagraphCenter

.Replacement.Text = "[LI]^&[\LI]"
'**********************
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute replace:=wdReplaceAll
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi everybody,
I got my macro recorded so I can replace my text, but for some
reason, when I run it, it does nothing. I know Microsoft Word has
some weird bugs in it that doesn't always show paragraph and font
formatting when you replace, so I tried to fix it and it doesn't look
like I was sucessful. Anyway, can anyone tell me where I need to fix
this macro?

Thanks again!
Jezzica85

Sub Tagger()
' Replace centered, otherwise unformatted text with itself,
surrounded by [C] tags, bolded
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[C]^&[/C]"
.Replacement.Font.Bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace centered italicized text with itself, surrounded by [CI]
tags Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[CI]^&[/CI]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace left aligned italicized text with itself, surrounded by
[LI] tags Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[LI]^&[/LI]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace left aligned, otherwise unformatted text with itself,
surrounded by [L] tags
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[L]^&[/L]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
G

Guest

Oh, and one thing I forgot to add--I think it's all sort of the same problem
in each of the four different sections.
 
G

Guest

Thank you for the hint Graham, I figured this out now. I'll remember that
same thread thing in the future.
Jezzica85

Graham Mayor said:
It helps if you stick to the same thread for the same problem!
Your macro does nothing because it searches for nothing and replaces with
something. The macro recorder is not capable of recording all actions and
recording formatting is one of the things it cannot do. An example of a
macro to find formatted text (and replace witha text string) is shown below.
I have inserted the alternative lines so you can experiment. Note that the
tags may not go where you might expect.

Sub ReplaceExample()

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
'**********************
.Text = ""
.Font.Italic = True
'.Font.Italic = False
.ParagraphFormat.Alignment = wdAlignParagraphLeft
'.ParagraphFormat.Alignment=wdAlignParagraphCenter

.Replacement.Text = "[LI]^&[\LI]"
'**********************
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute replace:=wdReplaceAll
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi everybody,
I got my macro recorded so I can replace my text, but for some
reason, when I run it, it does nothing. I know Microsoft Word has
some weird bugs in it that doesn't always show paragraph and font
formatting when you replace, so I tried to fix it and it doesn't look
like I was sucessful. Anyway, can anyone tell me where I need to fix
this macro?

Thanks again!
Jezzica85

Sub Tagger()
' Replace centered, otherwise unformatted text with itself,
surrounded by [C] tags, bolded
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[C]^&[/C]"
.Replacement.Font.Bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace centered italicized text with itself, surrounded by [CI]
tags Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[CI]^&[/CI]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace left aligned italicized text with itself, surrounded by
[LI] tags Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[LI]^&[/LI]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Replace left aligned, otherwise unformatted text with itself,
surrounded by [L] tags
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[L]^&[/L]"
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 

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