Find and Replace

I

Island Girl

While trying to replace all company names in a really long document with
"XXX," it dawned on me that it would be helpful to find two or more words in
a row that begin with capital letters. Greg Maxey set me straight on a
question re word beginnings last week, and this carries that question bit
further.

I know how to find words that start with caps, but how do I find two or more
in a row? I can't figure out where to put the {2,} so that it will find the
words side by side instead of several words apart--which is the result I've
been getting.

I just love this stuff, and it's all because of you, my friends!
 
J

Jay Freedman

While trying to replace all company names in a really long document with
"XXX," it dawned on me that it would be helpful to find two or more words in
a row that begin with capital letters. Greg Maxey set me straight on a
question re word beginnings last week, and this carries that question bit
further.

I know how to find words that start with caps, but how do I find two or more
in a row? I can't figure out where to put the {2,} so that it will find the
words side by side instead of several words apart--which is the result I've
been getting.

I just love this stuff, and it's all because of you, my friends!

You can't use {2,} in this case. If you tried to say "two or more" that way, it
would only find instances where the second word is the same as the first word
(like "Walla Walla"). I don't think there are many company names like that. :)

You can use this for two words at a time

<[A-Z][a-z]@ [A-Z][a-z]@>

and this for three at a time

<[A-Z][a-z]@ [A-Z][a-z]@ [A-Z][a-z]@>

Unfortunately, trying to extend that to four words results in an "expression is
too complex" error, but you can repeat the two- and three-word searches until no
more are found.
 
G

Greg Maxey

Jay,

Hmm. I didn't try it with more than two so I will take your word for it.
This looks very clunky, but seems to work with limited testing:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim bProcess As Boolean
With Selection
.HomeKey wdStory
With .Find
.Text = "<[A-Z][a-z]@>"
.MatchWildcards = True
While .Execute
bProcess = False
Do While Selection.Words(Selection.Words.Count).Next.Characters(1) Like
"[A-Z]"
Set oRng = Selection.Range
oRng.End = oRng.Words(Selection.Words.Count).Next.End
oRng.Expand wdWord
oRng.Select
bProcess = True
Loop
If bProcess Then
If oRng.Characters.Last.Next Like "[.,!?:;]" Then
oRng.Text = "XXXXX"
Else
oRng.Text = "XXXXX "
End If
oRng.Collapse wdCollapseEnd
End If
Wend
End With
End With
End Sub


Jay said:
While trying to replace all company names in a really long document
with "XXX," it dawned on me that it would be helpful to find two or
more words in a row that begin with capital letters. Greg Maxey set
me straight on a question re word beginnings last week, and this
carries that question bit further.

I know how to find words that start with caps, but how do I find two
or more in a row? I can't figure out where to put the {2,} so that
it will find the words side by side instead of several words
apart--which is the result I've been getting.

I just love this stuff, and it's all because of you, my friends!

You can't use {2,} in this case. If you tried to say "two or more"
that way, it would only find instances where the second word is the
same as the first word (like "Walla Walla"). I don't think there are
many company names like that. :)

You can use this for two words at a time

<[A-Z][a-z]@ [A-Z][a-z]@>

and this for three at a time

<[A-Z][a-z]@ [A-Z][a-z]@ [A-Z][a-z]@>

Unfortunately, trying to extend that to four words results in an
"expression is too complex" error, but you can repeat the two- and
three-word searches until no more are found.
 
G

Greg Maxey

Jay,

Studying this a little more it becomes problematic when initials are
involved (this might not be an issue for IG). This can be overcome by first
converting initials and their assoiciated period to XX. I adapted the
earlier code to:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim oRngProcess As Word.Range
Set oRng = ActiveDocument.Range
ProcessInitials oRng, "[A-Z]."
With oRng.Find
.Text = "<[A-Z]* "
.MatchWildcards = True
While .Execute
Do While oRng.Words(oRng.Words.Count).Next.Characters(1) Like "[A-Z]"
With oRng
.End = .Words(oRng.Words.Count).Next.End
.Expand wdWord
End With
Set oRngProcess = oRng
Loop
If Not oRngProcess Is Nothing Then
If oRng.Characters.Last.Next Like "[.,!?:;]" Then
oRng.Text = "XXXXX"
Else
oRng.Text = "XXXXX "
End If
oRng.Collapse wdCollapseEnd
Set oRngProcess = Nothing
End If
Wend
End With
End Sub
Sub ProcessInitials(ByRef oRng As Word.Range, pStr As String)
With oRng.Find
.Text = pStr
.MatchWildcards = True
.Replacement.Text = "XX"
.Execute Replace:=wdReplaceAll
End With
End Sub

Still clunky, but seems to work for everything except possibly a single CAP
and period ending a sentence.



Jay said:
While trying to replace all company names in a really long document
with "XXX," it dawned on me that it would be helpful to find two or
more words in a row that begin with capital letters. Greg Maxey set
me straight on a question re word beginnings last week, and this
carries that question bit further.

I know how to find words that start with caps, but how do I find two
or more in a row? I can't figure out where to put the {2,} so that
it will find the words side by side instead of several words
apart--which is the result I've been getting.

I just love this stuff, and it's all because of you, my friends!

You can't use {2,} in this case. If you tried to say "two or more"
that way, it would only find instances where the second word is the
same as the first word (like "Walla Walla"). I don't think there are
many company names like that. :)

You can use this for two words at a time

<[A-Z][a-z]@ [A-Z][a-z]@>

and this for three at a time

<[A-Z][a-z]@ [A-Z][a-z]@ [A-Z][a-z]@>

Unfortunately, trying to extend that to four words results in an
"expression is too complex" error, but you can repeat the two- and
three-word searches until no more are found.
 
J

Jay Freedman

Thanks for following up, Greg. I went for the minimum assumption, figuring
that there would be a further question if there were complications such as
this.

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

Island Girl

For the hundredth time--or at least it seems like it--thanks a million, Jay!
I appreciate all the help you've given me over the years.

Jay Freedman said:
While trying to replace all company names in a really long document with
"XXX," it dawned on me that it would be helpful to find two or more words in
a row that begin with capital letters. Greg Maxey set me straight on a
question re word beginnings last week, and this carries that question bit
further.

I know how to find words that start with caps, but how do I find two or more
in a row? I can't figure out where to put the {2,} so that it will find the
words side by side instead of several words apart--which is the result I've
been getting.

I just love this stuff, and it's all because of you, my friends!

You can't use {2,} in this case. If you tried to say "two or more" that way, it
would only find instances where the second word is the same as the first word
(like "Walla Walla"). I don't think there are many company names like that. :)

You can use this for two words at a time

<[A-Z][a-z]@ [A-Z][a-z]@>

and this for three at a time

<[A-Z][a-z]@ [A-Z][a-z]@ [A-Z][a-z]@>

Unfortunately, trying to extend that to four words results in an "expression is
too complex" error, but you can repeat the two- and three-word searches until no
more are found.

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

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