![]() |
|
#1
|
|||
|
|||
|
Puzzled by behavior of "<" in a wildcard search. Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.Text = "An ax, a mule and an apple."
With oRng.Find
'This will find "only complete words" starting with a vowel.
.Text = "<[AEIOUaeiou]*>"
.MatchWildcards = True
While .Execute
oRng.Select
Wend
End With
Set oRng = ActiveDocument.Range
With oRng.Find
'So why doens't this find "only complete words" ending with a vowel.
.Text = "<*[AEIOUaeiou]>"
.MatchWildcards = True
While .Execute
oRng.Select
Wend
End With
lbl_Exit:
Exit Sub
End Sub
Here is a workaround but curious as to why the "<" seems to be ignored in second run above. Code:
Sub KlunkyWordAround()
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.Text = "An ax, a mule and an apple."
Set oRng = ActiveDocument.Range
With oRng.Find
'This will find "only complete words" ending with a vowel.
.Text = "[! ]@[AEIOUaeiou]>"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
oRng.Select
If oRng.End = 0 Then Exit Do
Loop
End With
lbl_Exit:
Exit Sub
End Sub
|
|
#2
|
||||
|
||||
|
The closest string to this that I could find is
.Text = "<[A-z]@[AEIOUaeiou]@>" but it misses the words with one letter like 'a' or 'I'
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#3
|
||||
|
||||
|
It is not ignored. The problem is the * which is a very blunt instrument. The search runs from the opening of a word until a vowel ends a word.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#4
|
|||
|
|||
|
Andrew/Graham
Thanks. This is just a tinkering exercise. The objective was to find and highlight words starting and ending with a vowel blue, words starting with a vowel yellow and words ending with a vowel green. Of course there are complications (e.g., compound words) ![]() Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.HighlightColorIndex = wdAuto
oRng.Text = "Mary has an ax, a mule and an apple." & vbCr & "Tom conducted a low-grade in-depth on-site inspection."
With oRng.Find
'Finds words of three characters or more that starts and ends with a vowel.
.Text = "<[AEIOUaeiou][A-Za-z]@[AEIOUaeiou]>"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
oRng.HighlightColorIndex = wdBlue
oRng.Collapse wdCollapseEnd
Loop
End With
Set oRng = ActiveDocument.Range
With oRng.Find
'Find words starting with a vowel
.Text = "<[AEIOUaeiou]*>"
.MatchWildcards = True
While .Execute
oRng.Select
If oRng.HighlightColorIndex = wdAuto Then
Select Case True
Case oRng.Words.Last.Next.Text = "-"
'Deal with compound words.
oRng.MoveEnd wdWord, 2
Do While oRng.Characters.Last = " "
oRng.End = oRng.End - 1
Loop
If oRng.Characters.Last Like "[AEIOUaeiou]" Then
oRng.HighlightColorIndex = wdBlue
Else
oRng.HighlightColorIndex = wdYellow
End If
Case oRng.Text Like "[AaI]"
'Deal with the single character words A, I and a.
oRng.HighlightColorIndex = wdBlue
Case Len(oRng) = 2 And oRng.Characters(2) Like "[AEIOUaeiou]"
'Unlikely but deals with "Oo" "ee" etc.
oRng.HighlightColorIndex = wdBlue
Case Else: oRng.HighlightColorIndex = wdYellow
End Select
End If
oRng.Collapse wdCollapseEnd
Wend
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A-z]@[AEIOUaeiou]>"
.MatchWildcards = True
.Wrap = wdFindStop
While .Execute
If oRng.HighlightColorIndex = wdAuto Then
Select Case True
'Deal with compound words.
Case oRng.Words.First.Previous.Text = "-": oRng.MoveStart wdWord, -2
End Select
oRng.HighlightColorIndex = wdBrightGreen
oRng.Collapse wdCollapseEnd
End If
Wend
End With
lbl_Exit:
Exit Sub
End Sub
|
|
#5
|
||||
|
||||
|
It looks like the Covid-19 lockdown is getting to you Greg. I thought it was a strange coincidence when the other thread appeared after yours.
Ignoring the compound words complication, the code could be as below. I think you could even add a \- into the search strings to include the compound words. Code:
Sub ScratchMacro2()
Dim oRng As Range
Set oRng = ActiveDocument.Range
oRng.HighlightColorIndex = wdAuto
oRng.Text = "Mary has an ax, a mule and an apple." & vbCr & "Tom conducted a low-grade in-depth on-site inspection."
With oRng.Find
.Text = "<[AEIOUaeiou]*>" 'starts with a vowel
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
Select Case lcase(Right(Trim(oRng), 1))
Case "a", "e", "i", "o", "u" 'also ends with a vowel
oRng.HighlightColorIndex = wdBlue
Case Else 'starts but doesn't end with a vowel
oRng.HighlightColorIndex = wdYellow
End Select
oRng.Collapse wdCollapseEnd
Loop
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "<[A-z]@[AEIOUaeiou]>" 'Find words starting with a vowel
.MatchWildcards = True
.Highlight = False 'and not already highlighted
While .Execute
oRng.Select
oRng.HighlightColorIndex = wdBrightGreen
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Want to do single wildcard search for Z?Z or Z??Z
|
MikeForward | Word | 9 | 02-19-2019 01:27 AM |
Having problems with a Wildcard search
|
Cosmo | Word VBA | 9 | 02-12-2016 08:03 PM |
| Issue with wildcard search | mysterytramp | Word | 0 | 05-13-2015 10:40 AM |
Tricky wildcard search
|
NobodysPerfect | Word | 10 | 03-19-2014 04:29 AM |
| Wildcard search help. | Kempston | Word | 0 | 11-13-2009 03:58 AM |