Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find uppercase text 1

Status
Not open for further replies.

dasmotiu

Programmer
Nov 30, 2010
8
0
0
US
I have a need (I wont bother you with why) to find the next occurrence of three or more adjacent capital letters in the text of a Word document. I know I can loop through the text character by character but that approach is unbelievably slow. Is there a better method? I am using Office 2007.
 
With Selection.Find
.Text = "[A-Z][A-Z][A-Z]"
.MatchWildcards = True
End With
Selection.Find.Execute
 
You want to find three characters, then three characters is what you have to look for.

So, if the next found UpperCase character is followed by lowercase, you do not want it. Correct? You want three uppercase characters, togther, no spaces. Correct?

Are those three uppercase characters followed by a space? A lowercase character?

If they are three characters, followed by a space, then you CAN search for "words", rather than single characters.

If the three uppercase characters are followed by a lowercase character, then you could do something like:
Code:
Dim r As Range
Set r = ActiveDocument.Range
   With r.Find
      .ClearFormatting
      .MatchWildcards = True
      .Text = "[A-Z][A-Z][A-Z]*"
      Do While .Execute(Forward:=True) = True
        ' whatever you want with the word
With:

The QUIick BRown fox jumps over the lazy dog.
The quick brown fox jumps OVEr the lazy dog.

The code would find QUIck, OVEr.

BRown would NOT be included. Nor any of the "The".


unknown
 
Wow, is it that easy to use regular expressions in VBA? I though you had to shell out to something external like vbscript. I will try it.

One small question, how does VBA know the search string you specified is a regular expression and not just a string of characters to be searched? For example "." normally means look for a period, but it means something completely different when read as a regex.
 
The magic is here:
.MatchWildcards = True

WARNING: this is not regex but Word wildcars ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
BTW, no need to use any vba to do this. You can do this right in the find window.
 
That is correct. You do not need VBA to use wildcards. But you certainly can use wildcards with a VBA find operation.

As an alternative you could also use a word range. This is not all that better (in fact, it is not at all,I am including this for amusiment sake).
Code:
Sub AlternativeThreeUpper()
Dim aWord
Dim r As Range
For Each aWord In ActiveDocument.Range.Words()
   Set r = aWord.Duplicate
   If Len(r.Text) > 4 Then
      r.End = r.Start + 3
      If r.Case = 1 Then
        aWord.Font.Color = wdColorDarkRed
      End If
   End If
Next
End Sub
In the above all words starting with three uppercase characters are turned to DarkRed.

Note the use of Duplicate. It will NOT work correctly if you do not use Duplicate. If you do not use Duplicate changing the range to test only rthe first three characters changes the range of aWord to be only those three characters.


unknown
 
Personally, I'd make the find expression:
[A-Z]{3}
Note also that you can specify that the find string must start a word (Find = <[A-Z]{3}), end a word (Find = [A-Z]{3}>), or be a complete word (Find = <[A-Z]{3}>).

As for needing '.Duplicate', consider the following two subs, neither of which use it:
Code:
Sub Demo()
Dim fRng As Range
With Selection.Find
  .ClearFormatting
  .Text = "[A-Z]{3}"
  .MatchWildcards = True
  .Wrap = wdFindContinue
  .Forward = True
  Do While .Execute = True
    Set fRng = ActiveDocument.Range(Start:=Selection.Start, End:=Selection.End)
    If Len(fRng.Words(1)) > 4 And fRng.Words(1).Case = 1 Then _
      fRng.Words(1).Font.Color = wdColorDarkRed
  Loop
End With
Set fRng = Nothing
End Sub
and
Code:
Sub AlternativeThreeUpper()
Dim oRng As Range
For Each oRng In ActiveDocument.Range.Words
  With oRng
    If Len(.Text) > 4 And .Case = 1 Then .Font.Color = wdColorDarkRed
  End With
Next
End Sub


Cheers
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top