wildcard searches

K

Keith G Hicks

Word 2003.

I'm trying to create a macro to search with word's regex but word's regex
seems pretty limited and I'm having trouble. HEre's what I need to do:

I need to find insteances like the following:

Section I Green
Section I Blue
Section II Green
Section II Blue
Section III Green
Section III Blue
Section IV Green
Section IV Blue
etc.


Then I want to replace each of the above with the same text but Bold,
Underlined and with a carriage return after each one so that there's a blank
line below. There could be any number of sections. I have no way to predict
how many there could be. Of course I could write code to handle up to a
certain number (say 20 for example) and just hard code them in but that
seems silly.

One other thing. I used the "record macro" feature to do a search and then
bold/underline/return (but without the wildcard/regex idea). While I was
recording the macro, the text did turn bold/underline and had the return.
But then when I actually ran the code that was recorded, only the return
worked. The text was not bolded or underlined. Why is this?

I know I could do this in vb.net using RegEx but I'm trying to avoid writing
a vb.net app to do this.

Hoping someone can help,

Keith
 
J

Jay Freedman

For using wildcards, see http://www.gmayor.com/replace_using_wildcards.htm.
By the way, it's a mistake to think of Word's wildcard syntax as RegEx. They
have some similarities, but there are enough differences to drive you crazy.
Still, it's entirely adequate for the task you described.

Once you set up the Find/Replace as a wildcard search, you don't have to
worry about how many instances there are; just tell it to Replace All.

As far as the recorded macro not working, that's a bug in the recorder. See
the "Fixing broken Replace macros" section in
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

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

Keith G Hicks

Well that helped but I can't figure out the "OR" for whole words:

Section [0-9IVX]{1,2} Blue

The above will find things like "Section 9 Blue" or "Section IX Blue"

Section [0-9IVX]{1,2} Green

And the above will find things like "Section 9 Green" or "Section IX Green"

But how do I "OR" the Blue and Green so I don't need to separate searches?
 
G

Graham Mayor

The short answer is that you can't do that in the Find dialog. What you
could do instead is search for Section and the roman numeral and assign it
to a range. then see what comes next and if it is Blue or Green, add the
next word to the range. You can then do what you want with the range e.g.

Dim oRng As Range
With Selection
.HomeKey wdStory
With .Find
While .Execute(findText:="Section [IVX]{1,3}", _
MatchWildcards:=True)
Set oRng = Selection.Range
If oRng.Next(wdWord, 1) = "Blue" Or _
oRng.Next(wdWord, 1) = "Green" Then
oRng.End = oRng.Next(wdWord, 1).End
End If
MsgBox oRng
Wend
End With
End With

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


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


Keith G Hicks said:
Well that helped but I can't figure out the "OR" for whole words:

Section [0-9IVX]{1,2} Blue

The above will find things like "Section 9 Blue" or "Section IX Blue"

Section [0-9IVX]{1,2} Green

And the above will find things like "Section 9 Green" or "Section IX
Green"

But how do I "OR" the Blue and Green so I don't need to separate searches?


Jay Freedman said:
For using wildcards, see
http://www.gmayor.com/replace_using_wildcards.htm. By the way, it's a
mistake to think of Word's wildcard syntax as RegEx. They have some
similarities, but there are enough differences to drive you crazy. Still,
it's entirely adequate for the task you described.

Once you set up the Find/Replace as a wildcard search, you don't have to
worry about how many instances there are; just tell it to Replace All.

As far as the recorded macro not working, that's a bug in the recorder.
See the "Fixing broken Replace macros" section in
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

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

Jay Freedman

That's one of the places where Word wildcards are inferior to real
RegEx -- there is no OR function for whole words.

If all the occurrences of "Section [0-9IVX]{1,2}" in the document are
followed by either "Blue" or "Green", you could write a search term

Section [0-9IVX]{1,2} [BGelnur]{4,5}

This will match all the terms you want, but it would also match
"Section 9 Glue" or "Section 9 Burn". These seem unlikely, but that's
your call.

In the general case where you can't predict what's in the document,
you will have to use separate searches. There are techniques using
arrays to hold the desired words, so the search part only has to be
coded once.

Well that helped but I can't figure out the "OR" for whole words:

Section [0-9IVX]{1,2} Blue

The above will find things like "Section 9 Blue" or "Section IX Blue"

Section [0-9IVX]{1,2} Green

And the above will find things like "Section 9 Green" or "Section IX Green"

But how do I "OR" the Blue and Green so I don't need to separate searches?


Jay Freedman said:
For using wildcards, see
http://www.gmayor.com/replace_using_wildcards.htm. By the way, it's a
mistake to think of Word's wildcard syntax as RegEx. They have some
similarities, but there are enough differences to drive you crazy. Still,
it's entirely adequate for the task you described.

Once you set up the Find/Replace as a wildcard search, you don't have to
worry about how many instances there are; just tell it to Replace All.

As far as the recorded macro not working, that's a bug in the recorder.
See the "Fixing broken Replace macros" section in
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

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

Keith G Hicks

Well a little snag here. I could also have these:

Section I Blue & Green
Section I Blue&Green
Section II B&G
Section II B & G

wdWord doesn't work with the ampersand in the way. I can't figure out how to
find these:

Blue & Green
Blue&Green
B&G
B & G

I tride wdSentence, wdCharacter and looked over the help file and nothings
popping up that works.


Graham Mayor said:
The short answer is that you can't do that in the Find dialog. What you
could do instead is search for Section and the roman numeral and assign it
to a range. then see what comes next and if it is Blue or Green, add the
next word to the range. You can then do what you want with the range e.g.

Dim oRng As Range
With Selection
.HomeKey wdStory
With .Find
While .Execute(findText:="Section [IVX]{1,3}", _
MatchWildcards:=True)
Set oRng = Selection.Range
If oRng.Next(wdWord, 1) = "Blue" Or _
oRng.Next(wdWord, 1) = "Green" Then
oRng.End = oRng.Next(wdWord, 1).End
End If
MsgBox oRng
Wend
End With
End With

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


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


Keith G Hicks said:
Well that helped but I can't figure out the "OR" for whole words:

Section [0-9IVX]{1,2} Blue

The above will find things like "Section 9 Blue" or "Section IX Blue"

Section [0-9IVX]{1,2} Green

And the above will find things like "Section 9 Green" or "Section IX
Green"

But how do I "OR" the Blue and Green so I don't need to separate
searches?


Jay Freedman said:
For using wildcards, see
http://www.gmayor.com/replace_using_wildcards.htm. By the way, it's a
mistake to think of Word's wildcard syntax as RegEx. They have some
similarities, but there are enough differences to drive you crazy.
Still, it's entirely adequate for the task you described.

Once you set up the Find/Replace as a wildcard search, you don't have to
worry about how many instances there are; just tell it to Replace All.

As far as the recorded macro not working, that's a bug in the recorder.
See the "Fixing broken Replace macros" section in
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

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

Keith G Hicks wrote:
Word 2003.

I'm trying to create a macro to search with word's regex but word's
regex seems pretty limited and I'm having trouble. HEre's what I need
to do:
I need to find insteances like the following:

Section I Green
Section I Blue
Section II Green
Section II Blue
Section III Green
Section III Blue
Section IV Green
Section IV Blue
etc.


Then I want to replace each of the above with the same text but Bold,
Underlined and with a carriage return after each one so that there's
a blank line below. There could be any number of sections. I have no
way to predict how many there could be. Of course I could write code
to handle up to a certain number (say 20 for example) and just hard
code them in but that seems silly.

One other thing. I used the "record macro" feature to do a search and
then bold/underline/return (but without the wildcard/regex idea).
While I was recording the macro, the text did turn bold/underline and
had the return. But then when I actually ran the code that was
recorded, only the return worked. The text was not bolded or
underlined. Why is this?
I know I could do this in vb.net using RegEx but I'm trying to avoid
writing a vb.net app to do this.

Hoping someone can help,

Keith
 
K

Keith G Hicks

This seems to work

Dim oRng As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
While .Execute(findText:="Section {1,}[0-9IVX]{1,2} {1,}",
MatchWildcards:=True)
Set oRng = Selection.Range
If oRng.Next(wdWord, 1) = "Green" Then
oRng.End = oRng.Next(wdWord, 1).End
ElseIf Trim(oRng.Next(wdWord, 1)) = "B" And
Trim(oRng.Next(wdWord, 3)) = "B" Then 'satisfies "B & B" regardless of # of
spaces
oRng.End = oRng.Next(wdWord, 3).End
ElseIf Trim(oRng.Next(wdWord, 1)) = "Blue" And
Trim(oRng.Next(wdWord, 3)) = "Green" Then 'satisfies "Blue & Green"
regardless of # of spaces
oRng.End = oRng.Next(wdWord, 3).End
End If
oRng.Bold = True
oRng.Underline = wdUnderlineSingle
oRng.InsertAfter vbCrLf
'MsgBox oRng
Wend
End With
End With


Keith G Hicks said:
Well a little snag here. I could also have these:

Section I Blue & Green
Section I Blue&Green
Section II B&G
Section II B & G

wdWord doesn't work with the ampersand in the way. I can't figure out how
to find these:

Blue & Green
Blue&Green
B&G
B & G

I tride wdSentence, wdCharacter and looked over the help file and nothings
popping up that works.


Graham Mayor said:
The short answer is that you can't do that in the Find dialog. What you
could do instead is search for Section and the roman numeral and assign
it to a range. then see what comes next and if it is Blue or Green, add
the next word to the range. You can then do what you want with the range
e.g.

Dim oRng As Range
With Selection
.HomeKey wdStory
With .Find
While .Execute(findText:="Section [IVX]{1,3}", _
MatchWildcards:=True)
Set oRng = Selection.Range
If oRng.Next(wdWord, 1) = "Blue" Or _
oRng.Next(wdWord, 1) = "Green" Then
oRng.End = oRng.Next(wdWord, 1).End
End If
MsgBox oRng
Wend
End With
End With

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


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


Keith G Hicks said:
Well that helped but I can't figure out the "OR" for whole words:

Section [0-9IVX]{1,2} Blue

The above will find things like "Section 9 Blue" or "Section IX Blue"

Section [0-9IVX]{1,2} Green

And the above will find things like "Section 9 Green" or "Section IX
Green"

But how do I "OR" the Blue and Green so I don't need to separate
searches?


For using wildcards, see
http://www.gmayor.com/replace_using_wildcards.htm. By the way, it's a
mistake to think of Word's wildcard syntax as RegEx. They have some
similarities, but there are enough differences to drive you crazy.
Still, it's entirely adequate for the task you described.

Once you set up the Find/Replace as a wildcard search, you don't have
to worry about how many instances there are; just tell it to Replace
All.

As far as the recorded macro not working, that's a bug in the recorder.
See the "Fixing broken Replace macros" section in
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm.

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

Keith G Hicks wrote:
Word 2003.

I'm trying to create a macro to search with word's regex but word's
regex seems pretty limited and I'm having trouble. HEre's what I need
to do:
I need to find insteances like the following:

Section I Green
Section I Blue
Section II Green
Section II Blue
Section III Green
Section III Blue
Section IV Green
Section IV Blue
etc.


Then I want to replace each of the above with the same text but Bold,
Underlined and with a carriage return after each one so that there's
a blank line below. There could be any number of sections. I have no
way to predict how many there could be. Of course I could write code
to handle up to a certain number (say 20 for example) and just hard
code them in but that seems silly.

One other thing. I used the "record macro" feature to do a search and
then bold/underline/return (but without the wildcard/regex idea).
While I was recording the macro, the text did turn bold/underline and
had the return. But then when I actually ran the code that was
recorded, only the return worked. The text was not bolded or
underlined. Why is this?
I know I could do this in vb.net using RegEx but I'm trying to avoid
writing a vb.net app to do this.

Hoping someone can help,

Keith
 

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