Change Pipe Character to Square Brackets Find Replace Loop

R

Rose

How do I replace Pipe |NAME| character with [NAME] square brackets in below
macro:

Stuck!

Option Explicit

Public Sub FindReplacePipe()
'Find replace [ ]
Dim replacement As String
replacement = InputBox("Enter the replacement text", "Replacer")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="|[A-z]{1,}|", ReplaceWith:=replacement,
MatchWildcards:=True, Wrap:=wdFindContinue, Forward:=True) = True
Loop
End With

End Sub
 
T

Tony Jollans

I'm not entirely sure what you're asking but to find square brackets in a
wildcard search you need to escape them ...

FindText:="\[[A-z]{1,}\]"

Or are you asking how to change pipes to square brackets in the document at
the same time as your other change? In that case, try this ...

FindText:="|[A-z]{1,}|", ReplaceWith:="[" & replacement & "]"

Or have I completely missed the point?
 
D

Dave Lett

You were close:

Dim sReplace As String
sReplace = InputBox("Enter the replacement text", "Replacer")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting
.Text = "|([A-z]{1,})|"
.MatchWildcards = True
.Wrap = wdFindContinue
.Forward = True
With .replacement
.ClearFormatting
.Text = sReplace & "\1" & sReplace
End With
.Execute Replace:=wdReplaceAll
End With

However, sReplace (I changed this from "replacement" because "Replacement"
is actually a parameter in the Find process) doesn't work the way you
probably want it to. Therefore, you will probably want to hard code the
replacement character, as in the following:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting
.Text = "|([A-z]{1,})|"
.MatchWildcards = True
.Wrap = wdFindContinue
.Forward = True
With .replacement
.ClearFormatting
.Text = "[\1]" End With
.Execute Replace:=wdReplaceAll
End With

HTH,
Dave
 
R

Rose

Yes but close just doesn't work - MANY tks
Dave Lett said:
You were close:

Dim sReplace As String
sReplace = InputBox("Enter the replacement text", "Replacer")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting
.Text = "|([A-z]{1,})|"
.MatchWildcards = True
.Wrap = wdFindContinue
.Forward = True
With .replacement
.ClearFormatting
.Text = sReplace & "\1" & sReplace
End With
.Execute Replace:=wdReplaceAll
End With

However, sReplace (I changed this from "replacement" because "Replacement"
is actually a parameter in the Find process) doesn't work the way you
probably want it to. Therefore, you will probably want to hard code the
replacement character, as in the following:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting
.Text = "|([A-z]{1,})|"
.MatchWildcards = True
.Wrap = wdFindContinue
.Forward = True
With .replacement
.ClearFormatting
.Text = "[\1]" End With
.Execute Replace:=wdReplaceAll
End With

HTH,
Dave

Rose said:
How do I replace Pipe |NAME| character with [NAME] square brackets in
below macro:

Stuck!

Option Explicit

Public Sub FindReplacePipe()
'Find replace [ ]
Dim replacement As String
replacement = InputBox("Enter the replacement text", "Replacer")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="|[A-z]{1,}|", ReplaceWith:=replacement,
MatchWildcards:=True, Wrap:=wdFindContinue, Forward:=True) = True
Loop
End With

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