Selection using Wildcards

E

Edward Thrashcort

I'm trying to select all the phrases in a document that are written in a
particular character style and replace them with a PREFIX followed by a
SUFFIX. So let's assume that I have specified a character font for
numerals and assigned it only to the numbers in the following range...

abc 123 xyz 999 pqr

it would be replaced with

abc PREFIX123456SUFFIX wxy PREFIX999SUFFIX pqr

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.Forward = True
.Wrap = wdFindContinue

.Style = ActiveDocument.Styles("Numeral Font")
.Text = "(*)"
.Replacement.Text = "PREFIX\1SUFFIX"

'For testing I'll replace both separately
'The code should be .Execute Replace:=wdReplaceAll

.Execute Replace:=wdReplaceOne
.Execute Replace:=wdReplaceOne
End With

Unfortunately this code only selects the FIRST character then the SECOND
character - not the first phrase and the second phrase

I've also tried
.Text = "(*@)"
.Text = "(?@)"
I've tried
.Format = False
.Format = True (not quite sure what this means anyway!)

but nothing works!

Am I trying to do something that "wildcards" is not capable of performing?

Eddie
 
G

Greg Maxey

Try:
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = Selection.Range 'or ActiveDocument.Range
With oRng.Find
.Text = "[0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindContinue
.Style = ActiveDocument.Styles("Numeral Font")
.Replacement.Text = "PREFIX^&SUFFIX"
.Execute Replace:=wdReplaceAll
End With
End Sub
 
E

Edward Thrashcort

Thanks Greg I'll no go and examine the Help file to understand the meaning
of "[0-9]{1,}" particularly "{1,}" part

However in my example, I used numerals just to make the problem clearer.
In reality the font that I'm searching-for could contain ANYTHING, maybe
even pictures, tables and stuff.

Eddie
*From:* "Greg Maxey" <[email protected]>
*Date:* 23 Mar 2007 12:23:49 -0700

Try:
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = Selection.Range 'or ActiveDocument.Range
With oRng.Find
.Text = "[0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindContinue
.Style = ActiveDocument.Styles("Numeral Font")
.Replacement.Text = "PREFIX^&SUFFIX"
.Execute Replace:=wdReplaceAll
End With
End Sub

I'm trying to select all the phrases in a document that are written in a
particular character style and replace them with a PREFIX followed by a
SUFFIX. So let's assume that I have specified a character font for
numerals and assigned it only to the numbers in the following range...

abc 123 xyz 999 pqr

it would be replaced with

abc PREFIX123456SUFFIX wxy PREFIX999SUFFIX pqr

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.Forward = True
.Wrap = wdFindContinue

.Style = ActiveDocument.Styles("Numeral Font")
.Text = "(*)"
.Replacement.Text = "PREFIX\1SUFFIX"

'For testing I'll replace both separately
'The code should be .Execute Replace:=wdReplaceAll

.Execute Replace:=wdReplaceOne
.Execute Replace:=wdReplaceOne
End With

Unfortunately this code only selects the FIRST character then the SECOND
character - not the first phrase and the second phrase

I've also tried
.Text = "(*@)"
.Text = "(?@)"
I've tried
.Format = False
.Format = True (not quite sure what this means anyway!)

but nothing works!

Am I trying to do something that "wildcards" is not capable of
performing?

Eddie
 
G

Graham Mayor

{1,} means at least 1
http://www.gmayor.com/replace_using_wildcards.htm

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Edward said:
Thanks Greg I'll no go and examine the Help file to understand the
meaning of "[0-9]{1,}" particularly "{1,}" part

However in my example, I used numerals just to make the problem
clearer. In reality the font that I'm searching-for could contain
ANYTHING, maybe even pictures, tables and stuff.

Eddie
*From:* "Greg Maxey" <[email protected]>
*Date:* 23 Mar 2007 12:23:49 -0700

Try:
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = Selection.Range 'or ActiveDocument.Range
With oRng.Find
.Text = "[0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindContinue
.Style = ActiveDocument.Styles("Numeral Font")
.Replacement.Text = "PREFIX^&SUFFIX"
.Execute Replace:=wdReplaceAll
End With
End Sub

I'm trying to select all the phrases in a document that are written
in a particular character style and replace them with a PREFIX
followed by a SUFFIX. So let's assume that I have specified a
character font for numerals and assigned it only to the numbers in
the following range...

abc 123 xyz 999 pqr

it would be replaced with

abc PREFIX123456SUFFIX wxy PREFIX999SUFFIX pqr

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.Forward = True
.Wrap = wdFindContinue

.Style = ActiveDocument.Styles("Numeral Font")
.Text = "(*)"
.Replacement.Text = "PREFIX\1SUFFIX"

'For testing I'll replace both separately
'The code should be .Execute Replace:=wdReplaceAll

.Execute Replace:=wdReplaceOne
.Execute Replace:=wdReplaceOne
End With

Unfortunately this code only selects the FIRST character then the
SECOND character - not the first phrase and the second phrase

I've also tried
.Text = "(*@)"
.Text = "(?@)"
I've tried
.Format = False
.Format = True (not quite sure what this means anyway!)

but nothing works!

Am I trying to do something that "wildcards" is not capable of
performing?

Eddie
 
G

Greg Maxey

Edward,

In that case don't include the .Text line at all. You are simply looking
for a style.

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = Selection.Range 'or ActiveDocument.Range
With oRng.Find
.MatchWildcards = True
.Wrap = wdFindContinue
.Style = ActiveDocument.Styles("Numeral Font")
.Replacement.Text = "PREFIX^&SUFFIX"
.Execute Replace:=wdReplaceAll
End With
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Edward said:
Thanks Greg I'll no go and examine the Help file to understand the
meaning of "[0-9]{1,}" particularly "{1,}" part

However in my example, I used numerals just to make the problem
clearer. In reality the font that I'm searching-for could contain
ANYTHING, maybe even pictures, tables and stuff.

Eddie
*From:* "Greg Maxey" <[email protected]>
*Date:* 23 Mar 2007 12:23:49 -0700

Try:
Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = Selection.Range 'or ActiveDocument.Range
With oRng.Find
.Text = "[0-9]{1,}"
.MatchWildcards = True
.Wrap = wdFindContinue
.Style = ActiveDocument.Styles("Numeral Font")
.Replacement.Text = "PREFIX^&SUFFIX"
.Execute Replace:=wdReplaceAll
End With
End Sub

I'm trying to select all the phrases in a document that are written
in a particular character style and replace them with a PREFIX
followed by a SUFFIX. So let's assume that I have specified a
character font for numerals and assigned it only to the numbers in
the following range...

abc 123 xyz 999 pqr

it would be replaced with

abc PREFIX123456SUFFIX wxy PREFIX999SUFFIX pqr

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.Forward = True
.Wrap = wdFindContinue

.Style = ActiveDocument.Styles("Numeral Font")
.Text = "(*)"
.Replacement.Text = "PREFIX\1SUFFIX"

'For testing I'll replace both separately
'The code should be .Execute Replace:=wdReplaceAll

.Execute Replace:=wdReplaceOne
.Execute Replace:=wdReplaceOne
End With

Unfortunately this code only selects the FIRST character then the
SECOND character - not the first phrase and the second phrase

I've also tried
.Text = "(*@)"
.Text = "(?@)"
I've tried
.Format = False
.Format = True (not quite sure what this means anyway!)

but nothing works!

Am I trying to do something that "wildcards" is not capable of
performing?

Eddie
 

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