PDA

View Full Version : how to find capital letters



dagerr
07-22-2019, 04:12 AM
Hi, I don't know what is wrong but I can't use or find capital letters using vba. Below simple code and text for example:

lorem ipsum
LOREM IPSUM
lorem ipsum


Sub capital()
'
Selection.Find.ClearFormatting
With Selection.Find.Font
.SmallCaps = False
.AllCaps = True
End With
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

Paul_Hossler
07-22-2019, 06:42 AM
For the "LOREM IPSUM" did you set the Font checkbox AllCaps = true or just type it it all caps?

dagerr
07-22-2019, 07:30 AM
Allcaps = true

gmaxey
07-22-2019, 10:02 AM
Your code seems to work here to find the CAPITALIZED instance.

Kilroy
07-22-2019, 10:36 AM
Didn't work here. (Word 016). Try this:


Sub HighlightCapitalWordsYellow()
Dim objRange As Range

With Selection
With Selection.Find
.ClearFormatting
.Text = "[A-Z]{1, }"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
Set objRange = Selection.Range
objRange.HighlightColorIndex = wdYellow
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Sub

dagerr
07-22-2019, 11:35 AM
Didn't work here. (Word 016). Try this:


Sub HighlightCapitalWordsYellow()
Dim objRange As Range

With Selection
With Selection.Find
.ClearFormatting
.Text = "[A-Z]{1, }"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
Set objRange = Selection.Range
objRange.HighlightColorIndex = wdYellow
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Sub

no, it is run with runtime error 5560.
Anyway, why

Allcaps = true
didn't work?

gmaxey
07-22-2019, 11:44 AM
The RTE is due to an extraneous space {1, } should be {1,} but that is going to find all capital letters e.g., the "T" in This and THAT.

Kilroy
07-22-2019, 11:59 AM
Greg doesn't the space after the comma mean "One or more" {1, } If you take the space out isn't that saying just find one?
Removing the space did work though. Thanks

gmaxey
07-22-2019, 12:13 PM
Kilroy,

No. I don't see how your code ran with that space. How do you have your break on errors set?

7. { }

Curly brackets are used for counting occurrences of the previous character or expression.
{n} This finds exactly the number “n” of occurrences of the previous character (so for example, a{2} will find “aa”).
{n,} finds at least the number “n” occurrences; so a{2,} will find “aa” and “aaaa”).
{n,m} finds text containing between “n” and “m” occurrences of the previous character or expression; so a{2,3} will find “aa” and “aaa”, but only the first 3 characters in “aaaa” ).
Note: Counting can be used with individual characters or more usefully with sets of characters e.g. [deno]{4} will match done, node, eden); or with bracketed groups: (ts, ){3} will match ts, ts, ts, .
(Unfortunately, there is no wildcard to search for “zero or more occurrences” in Word wildcard searches; [!^13]{0,} does not work).

Kilroy
07-22-2019, 12:36 PM
Don't know Greg. It worked earlier? as I stepped through. Then after seeing the responses I checked again and it didn't work. Thanks for the correction.