Consulting

Results 1 to 10 of 10

Thread: how to find capital letters

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location

    how to find capital letters

    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
    Karol

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,820
    Location
    For the "LOREM IPSUM" did you set the Font checkbox AllCaps = true or just type it it all caps?
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Allcaps = true
    Karol

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,063
    Location
    Your code seems to work here to find the CAPITALIZED instance.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    262
    Location
    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

  6. #6
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Quote Originally Posted by Kilroy View Post
    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?
    Karol

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,063
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    262
    Location
    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

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,063
    Location
    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).
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    262
    Location
    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.
    Last edited by Kilroy; 07-22-2019 at 12:39 PM. Reason: spelling

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •