find capitalized words and replace with a text

M

mark20090901

I need a macro that do this in MS Word 2007

in the selected part of text to:
- find any words whose all LETTERS are capitalized (so Canada for example
should not be found, but CANADA should be)
- and replaces all those instances with a text , for example string "HERE"

I wonder if anybody could help with that?

Mark
 
G

Greg Maxey

Mark,

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = Selection.Range
With oRng.Find
.Text = "<[A-Z]@>"
.MatchWildcards = True
.Replacement.Text = "HERE"
.Execute Replace:=wdReplaceAll
End With
End Sub

Note "I" in your example will also be replaced with "HERE"
 
T

Terry Farrell

Well if you use Find and Replace to simply find ANY instance of canada
regardless of capitalisation and Replace it with Canada, then it should do
what you want. It doesn't matter that it will find Canada and replace it,
because it replaces it with Canada anyway. Just keep the F&R dead simple.
 
M

mark20090901

Thanks a lot, I appreciate your help!

In the meantime I found this macro is working OK for me. (my intention was to
change any word in the selected text to a big dot, word "Canada" was just an
example here.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Size = 18
.Bold = True
.Color = wdColorRed
End With
With Selection.Find
.Text = "<[A-Z]*[A-Z]>"
.Replacement.Text = " • "
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.MoveDown Unit:=wdLine, Count:=1

Best regards,

Mark
 
M

mark20090901 via OfficeKB.com

Greg,

Thanks again for your help.
Your macro works perfectly. The only thing I still need how to put into it a
condition so when a uppercased word is "I" or "A" then those two cases should
NOT be replaced.
Only words having two or more uppercase letter should be replaced.

Mark

--------------------------------------

Greg said:
Mark,

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = Selection.Range
With oRng.Find
.Text = "<[A-Z]@>"
.MatchWildcards = True
.Replacement.Text = "HERE"
.Execute Replace:=wdReplaceAll
End With
End Sub

Note "I" in your example will also be replaced with "HERE"
I need a macro that do this in MS Word 2007
[quoted text clipped - 8 lines]
 
G

Greg Maxey

Mark,

Since A and I may be all that you would encounter in most case and to keep
it simple you might consider just processing all two letter CAP
combinations:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = Selection.Range
With oRng.Find
.Text = "<[A-Z]{2,}>"
.MatchWildcards = True
.Replacement.Text = "."
.Execute Replace:=wdReplaceAll
End With
End Sub

Or a more complicated approach:


Sub ScratchMacroII()
Dim oRng As Word.Range
Set oRng = Selection.Range
With oRng.Find
.Text = "<[A-Z]@>"
.MatchWildcards = True
While .Execute
If Len(oRng) > 2 Then
oRng.Text = "."
oRng.Collapse wdCollapseEnd
ElseIf Len(oRng) = 1 And Not oRng.Text = "A" And Not oRng.Text = "I"
Then
oRng.Text = "."
oRng.Collapse wdCollapseEnd
End If
Wend
End With
End Sub

Greg,

Thanks again for your help.
Your macro works perfectly. The only thing I still need how to put
into it a condition so when a uppercased word is "I" or "A" then
those two cases should NOT be replaced.
Only words having two or more uppercase letter should be replaced.

Mark

--------------------------------------

Greg said:
Mark,

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = Selection.Range
With oRng.Find
.Text = "<[A-Z]@>"
.MatchWildcards = True
.Replacement.Text = "HERE"
.Execute Replace:=wdReplaceAll
End With
End Sub

Note "I" in your example will also be replaced with "HERE"
I need a macro that do this in MS Word 2007
[quoted text clipped - 8 lines]
 
M

mark20090901 via OfficeKB.com

Thanks again Greg,

the second solution works perfectly for me. Here is an example why I needed
it. It allows me to quickly create the linguistic exercises like this:

the German battleship Schleswig-Holstein • a tiny Polish military outpost
?
the German battleship Schleswig-Holstein SHELLED a tiny Polish military
outpost

------------------------------------------------------------------

the country was used as a base for the Nazis' • machinery
?
the country was used as a base for the Nazis' GENOCIDE machinery

where the second line in each pair was the original one

Best regards,

Mark



Greg said:
Mark,

Since A and I may be all that you would encounter in most case and to keep
it simple you might consider just processing all two letter CAP
combinations:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = Selection.Range
With oRng.Find
.Text = "<[A-Z]{2,}>"
.MatchWildcards = True
.Replacement.Text = "."
.Execute Replace:=wdReplaceAll
End With
End Sub

Or a more complicated approach:

Sub ScratchMacroII()
Dim oRng As Word.Range
Set oRng = Selection.Range
With oRng.Find
.Text = "<[A-Z]@>"
.MatchWildcards = True
While .Execute
If Len(oRng) > 2 Then
oRng.Text = "."
oRng.Collapse wdCollapseEnd
ElseIf Len(oRng) = 1 And Not oRng.Text = "A" And Not oRng.Text = "I"
Then
oRng.Text = "."
oRng.Collapse wdCollapseEnd
End If
Wend
End With
End Sub
[quoted text clipped - 28 lines]
 
O

Octavio

When the new Windows 7 is expected to be on the retail stores for sale? Any
idea of the retail prices?

Will the Office Professional 2007 programs work well with it (I bet it will,
since they are from the same company. Any anticipated problems? Slowness
in old computers?)
 
T

Terry Farrell

Public release of Win 7 is due 22 October. You can pre-order Win 7 Home
Premium from Tesco for £70 ( with double club card points) now - only while
stock last. There's going to be a triple home pack introductory offer coming
on the shelves shortly for a limited period pre-offer (£150). Search the
usual suspects such as Amazon for the best offers. Pre-ordering will be
cheaper.

It will run on any PC that runs Vista but better. I have no problems with it
running on a Netbook. If it can run on a Netbook designed for Win XP, then
it will run on most low powered compatible PCs. You can download the
Compatibility Checker from Microsoft. You'll also find full details of
version and upgrades available on the MS site.
 

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