Word: Macro to change headings to sentence caseJuly 1, 2011
You have a long Word document where one or more authors have used various cases for the headings — sentence case (first letter of the first word is capitalized), title case (first letter of all words or all main words are capitalized), and upper case (all words are capitalized). It’s a mess and you need to change all headings to be sentence case.
Run a macro to change the case based on a heading style.
For example, run the macro once to change the case of all Heading 1s, then change the heading style (Heading 1 below) in the macro code and run it again to change all Heading 2s; repeat for Heading 3s, Heading 4s, etc.
There’s probably a more elegant solution to find and change all heading styles at once, but as this is likely to be something you only use a few times, the extra effort to change the heading style’s name before each run is not arduous.
Macro to change headings to sentence case, based on the heading style
Sub ChangeCase() With Selection.Find .ClearFormatting .Wrap = wdFindContinue .Forward = True .Format = True .MatchWildcards = False .Text = "" .Style = ActiveDocument.Styles("Heading 1") .Execute While .Found Selection.Range.Case = wdTitleSentence Selection.Collapse Direction:=wdCollapseEnd .Execute Wend End With End Sub
Sentence case is defined in the macro by wdTitleSentence, but what if you want something different? Substitute wdTitleSentence with one of these options:
- wdLowerCase — all lower case (probably not what you want as you’ll need an initial capital for a heading)
- wdTitleWord — every word in the heading has initial capitals (including all the little words like ‘is’, ‘the’, ‘a’, ‘an’, ‘by’, ‘for’, etc.)
- wdUpperCase — all upper case
There are other options too, but these are the main ones you’re likely to use.
If a macro is too scary for you, you can quickly change one heading at a time by selecting it, then pressing Shift+F3 one or more times in succession to toggle through the various cases.
[Links last checked June 2011; this macro is a modification of one from Allen Wyatt’s Word VBA Guidebook (http://store.tips.net/T010353_Word_VBA_Guidebook_Table_of_Contents.html)]