Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-11-2020, 11:22 AM
gmaxey gmaxey is online now Wildcard Search Windows 10 Wildcard Search Office 2016
Expert
Wildcard Search
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default Wildcard Search

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 07-11-2020, 07:09 PM
Guessed's Avatar
Guessed Guessed is offline Wildcard Search Windows 10 Wildcard Search Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 07-11-2020, 07:48 PM
gmayor's Avatar
gmayor gmayor is offline Wildcard Search Windows 10 Wildcard Search Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #4  
Old 07-12-2020, 06:37 AM
gmaxey gmaxey is online now Wildcard Search Windows 10 Wildcard Search Office 2016
Expert
Wildcard Search
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 07-12-2020, 05:13 PM
Guessed's Avatar
Guessed Guessed is offline Wildcard Search Windows 10 Wildcard Search Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wildcard Search Want to do single wildcard search for Z?Z or Z??Z MikeForward Word 9 02-19-2019 01:27 AM
Wildcard Search 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
Wildcard Search Tricky wildcard search NobodysPerfect Word 10 03-19-2014 04:29 AM
Wildcard search help. Kempston Word 0 11-13-2009 03:58 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:45 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft