Word: Macro to set the language for most elementsOctober 14, 2011
I needed to set the language for all elements of a Word 2007 template to English (Australia). I could set the language for the body of the document easily enough via a simple macro, but this didn’t set the language for the headers, footers, text boxes, or the styles (yes, you can set the language for a style!).
So I asked over at the Microsoft Answers forums and the ever-helpful Greg Maxey came up with this macro, which sets the language for all elements of the document, except the styles. As I had fewer than 20 styles in the template, setting the language for those manually wasn’t an arduous task.
Here’s Greg’s macro — change the language from wdEnglishAUS (two places) if you don’t want Australian English (links to the available languages after the macro).
Public Sub SetLangAllRanges() Dim rngStory As Word.Range Dim lngJunk As Long Dim oShp As Shape lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType For Each rngStory In ActiveDocument.StoryRanges 'Iterate through all linked stories Do On Error Resume Next rngStory.LanguageID = wdEnglishAUS Select Case rngStory.StoryType Case 6, 7, 8, 9, 10, 11 If rngStory.ShapeRange.Count > 0 Then For Each oShp In rngStory.ShapeRange If oShp.TextFrame.HasText Then oShp.TextFrame.TextRange.LanguageID = wdEnglishAUS End If Next End If Case Else 'Do Nothing End Select On Error GoTo 0 'Get next linked story (if any) Set rngStory = rngStory.NextStoryRange Loop Until rngStory Is Nothing Next End Sub
Languages available in Word, with their wd language ID code:
- Word 2007: http://msdn.microsoft.com/en-us/library/bb213877(v=office.12).aspx
- Word 2010: http://msdn.microsoft.com/en-us/library/ff834937.aspx
[Links last checked October 2011; if this macro has helped you, consider making a small donation of thanks to Greg at http://gregmaxey.mvps.org/word_tips.htm]