Find & replace with wildcards not working in macro

N

NavyPsych

I have a macro with a find and replace array. I haven't had any
problems with using wildcards until I tried to replace every instance
of "#-#" , i.e. "3-12" or "5-9" and simply replace the dash with "to",
i.e. change "3-12" to "3 to 12." Anyway, these find and replace terms
work perfectly in Word:

Find: ^32([0-9]{1})^45([0-9]{1,2}[!0-9])
Replace: \1 to \2

But, when I use the same in my VBA find and replace array, it doesn't
work. For example, if I have the sentence:

"Mike's parents separated when he was age 1-2."

If I simply find and replace within Word I get: "Mike's parents
separated when he was age 1 to 2."

But when I run the macro I get: "Mike's parents separated when he was
age12. to " <-- (two spaces before and after "to")

All my other wildcard strings in this array work without a problem.
Any ideas?
 
J

Jay Freedman

I tried this simpler code, since I don't know how you're handling the
array, and it works as expected:

Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^32([0-9]{1})^45([0-9]{1,2}[!0-9])"
.Replacement.Text = " \1 to \2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

Maybe if you post your complete macro, we can spot some reason for
your results.

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

NavyPsych

Thanks for your reply. I actually cut and pasted your code and ran it,
stand-alone, as its own macro, and I got the same thing "2-3" became
"23 to "

???
 
J

Jay Freedman

NavyPsych said:
Thanks for your reply. I actually cut and pasted your code and ran it,
stand-alone, as its own macro, and I got the same thing "2-3" became
"23 to "

???

Indeed, ???

I tried it again, in a fresh document and copy/pasting the code, and it
worked properly. I tried it on regular text and on text in a table. I tried
turning on/off various options in the Tools > Options dialog that I thought
might affect it. It still works.

Is the text that you're replacing in any place other than regular text? What
are your settings in Tools > Options > View and Tools > Options > Edit? Is
anything other than the default selected in Tools > Options > Compatibility?

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

Russ

NavyPsych,
Curious,
Have you tried recording a macro while using find and replace manually and
running that macro?
Is your array dimensioned as String?
What does your code look like where you are doing the find and replace and
where you are loading the array with values?
NavyPsych wrote:
Thanks for your reply. I actually cut and pasted your code and ran it,
stand-alone, as its own macro, and I got the same thing "2-3" became
"23 to "

???
I tried this simpler code, since I don't know how you're handling the
array, and it works as expected:

Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^32([0-9]{1})^45([0-9]{1,2}[!0-9])"
.Replacement.Text = " \1 to \2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

Maybe if you post your complete macro, we can spot some reason for
your results.

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

I have a macro with a find and replace array. I haven't had any
problems with using wildcards until I tried to replace every instance
of "#-#" , i.e. "3-12" or "5-9" and simply replace the dash with "to",
i.e. change "3-12" to "3 to 12." Anyway, these find and replace terms
work perfectly in Word:

Find: ^32([0-9]{1})^45([0-9]{1,2}[!0-9])
Replace: \1 to \2

But, when I use the same in my VBA find and replace array, it doesn't
work. For example, if I have the sentence:

"Mike's parents separated when he was age 1-2."

If I simply find and replace within Word I get: "Mike's parents
separated when he was age 1 to 2."

But when I run the macro I get: "Mike's parents separated when he was
age12. to " <-- (two spaces before and after "to")

All my other wildcard strings in this array work without a problem.
Any ideas?
 

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