0

Would like to ask for any advice on how to convert a sentence in to Title case with the exception of acronyms (or any words that is in all caps) in Microsoft Word. Currently my code only converts all the sentences in a specific style in Title case. Hoping you could help with with this. Thank you

Sub ChangeCase() StrFind = "K-1,K-2,K-3" 
      For i = 0 To UBound(Split(StrFind, ","))
     With Selection.Find
     .ClearFormatting
     .Wrap = wdFindContinue
     .Forward = True
     .Format = True
     .MatchWildcards = False
     .Text = ""
     .Style = Split(StrFind, ",")(i)
     .Execute
     While .Found
         Selection.Range.Case = wdTitleWord
         Selection.Collapse Direction:=wdCollapseEnd
        .Execute
     Wend
 End With
 Next i
 End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
JP0710
  • 41
  • 6
  • 2
    How could Word determine which word is an acronym and should not be Title case? Unless you give Word a list which words **not** to change there will be no solution for that. VBA cannot do any magic here (unless you code that magic). – Pᴇʜ Apr 14 '20 at 12:48
  • Yes as @Pᴇʜ is getting at, you will need to define a list of acronyms in your code and have your code exclude the formatting on those acronyms (perhaps using a loop over an If statement) - otherwise you could have your code check each character to determine if it's all caps (perhaps entering each word into an array (or something) and then checking each character one by one in a loop). – Samuel Everson Apr 14 '20 at 13:04
  • 1
    You don't. You allow Word to apply the title case to the acronym and then have a second function which restores the case of the acronyms. This does require you to maintain a list of acronyms. – freeflow Apr 14 '20 at 13:04
  • 1
    @SamuelEverson just for your interest you don't need to check each character of a word for upper case. A word is entirely upper case if `Ucase(word) = word`. – Pᴇʜ Apr 14 '20 at 13:08
  • Are all acronyms already in all caps? If so, this should be doable. – Ryan Wildry Apr 14 '20 at 14:35
  • @Ryan Wildry yes acronyms are already in all caps. Is there a way for VBA to determine those words that is already in all caps and will excluding it to convert into title case? – JP0710 Apr 14 '20 at 16:16
  • Hi - see the answer below. – Ryan Wildry Apr 14 '20 at 19:41

1 Answers1

2

If the objective is to TitleCase words that aren't already fully capitalized, I think the below should work.

Option Explicit

Public Sub TitleCaseDocument()
    Dim doc As Document: Set doc = ThisDocument
    Dim wrd As Range

    For Each wrd In doc.Words
        If wrd.Text <> UCase$(wrd.Text) Then wrd.Case = wdTitleWord
    Next
End Sub
Ryan Wildry
  • 5,612
  • 1
  • 15
  • 35
  • Thank you so much. However, is there any way to run this vba to specific word styles only and not on the whole document? – JP0710 Apr 15 '20 at 12:52
  • 1
    Yes, to check a style, try `Debug.Print wrd.Style` in the `For Each` loop. You can use a dictionary or similar to store the Style names you are interested in matching against. As for the second part, it would depend where you want to limit your search. If you get stuck, I think this merits a new question. – Ryan Wildry Apr 15 '20 at 14:06