Search code examples
vbams-worduppercase

Replace all uppercase text to smallcaps AND wdTitleSentence


I'm stuck with this problem for the past two days and I can't find a way to overcome it. I've a document (400 pages) where I want to replace ALL the uppercase words to SmallCaps AND set the text as "title sentence". When I register a macro, I found the commands that I need:

Selection.Range.Case = wdTitleSentence
Selection.Font.SmallCaps = wdToggle

The problem is that I can't find a way to apply these commands only to the uppercase words and NOT to the selected text.


Solution

  • You could try using a wildcard search, though you'll need to be careful how you specify it other wise you could change every capital letter in the document to small caps.

    Sub ConvertUpperCase()
       Dim findRange As Range
       Set findRange = ActiveDocument.Content
       
       With findRange.Find
          .ClearFormatting
          'find at least two consecutive capital letters
          .Text = "[A-Z]{2,}"
          .MatchWildcards = True
          Do While .Execute = True
             With findRange
                .Case = wdTitleSentence
                .Font.SmallCaps = True
                .Collapse wdCollapseEnd
             End With
          Loop
       End With
    End Sub