If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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. |
Thread Tools | |
Display Modes | |
|
|