Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.newusers
Island Girl Island Girl is offline
external usenet poster
 
Posts: 192
Default Find and Replace

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!
  #2   Report Post  
Posted to microsoft.public.word.newusers
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Find and Replace

On Sun, 8 Mar 2009 17:31:01 -0700, Island Girl
wrote:

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
  #3   Report Post  
Posted to microsoft.public.word.newusers
Greg Maxey[_2_] Greg Maxey[_2_] is offline
external usenet poster
 
Posts: 668
Default Find and Replace

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.Charac ters(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 Freedman wrote:
On Sun, 8 Mar 2009 17:31:01 -0700, Island Girl
wrote:

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.


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org



  #4   Report Post  
Posted to microsoft.public.word.newusers
Greg Maxey[_2_] Greg Maxey[_2_] is offline
external usenet poster
 
Posts: 668
Default Find and Replace

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 Freedman wrote:
On Sun, 8 Mar 2009 17:31:01 -0700, Island Girl
wrote:

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.


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org



  #5   Report Post  
Posted to microsoft.public.word.newusers
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Find and Replace

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Greg Maxey wrote:
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.





  #6   Report Post  
Posted to microsoft.public.word.newusers
Island Girl Island Girl is offline
external usenet poster
 
Posts: 192
Default Find and Replace

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" wrote:

On Sun, 8 Mar 2009 17:31:01 -0700, Island Girl
wrote:

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Find+Replace Dialog Box, Replace with Format problem Kathy Microsoft Word Help 3 February 1st 08 04:26 AM
Find multiple characters in one find using MSword find/replace Cliff Microsoft Word Help 2 October 29th 06 07:48 PM
Trying to replace words with fields using Find/Replace mbleyle Microsoft Word Help 2 March 29th 06 11:35 PM
Using find and replace or macros to replace page ranges JeremyC Microsoft Word Help 7 February 13th 06 09:20 PM
Find/ Replace is auto-capping the words I want to replace with Graham Mayor Microsoft Word Help 8 January 27th 06 01:39 AM


All times are GMT +1. The time now is 09:59 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"