Word: Macro to set the language for most elements
October 14, 2011I 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!). (Update September 2018: I now have a macro for setting the language for all styles at once: https://cybertext.wordpress.com/2018/09/21/word-macro-to-set-the-language-for-all-styles/)
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 and later (including 365): https://docs.microsoft.com/en-us/office/vba/api/Word.WdLanguageID
[Links last checked April 2021; if this macro has helped you, consider making a small donation of thanks to Greg at http://gregmaxey.mvps.org/word_tips.htm]
[…] newsletter/blog of CyberText Consulting – technical communication specialists « Word: Macro to set the language for most elements Word: Set the language for a style October 17, 2011 As a follow-up to my post about […]
by Word: Set the language for a style « CyberText Newsletter October 17, 2011 at 5:30 am[…] Word: Macro to set the language for most elements (cybertext.wordpress.com) Share this:DiggTwitterFacebookLike this:LikeBe the first to like this post. […]
by Sunday Cryptoquote Spoiler – 10/23/11 « Unclerave's Wordy Weblog October 24, 2011 at 12:09 amhey.. is it possible to find out english text in french document. i have avery huge document of 500 pages.. i want to run a macro to highlight the text if its in english.. is there any way.. because doing it manually will be very cumbersome
by nsj October 19, 2012 at 3:02 pmTwo things–you can (relatively) easily find paragraphs where the language is set to English, which might suit your needs (if the language was set in those paragraphs). With a macro, you might find paragraphs where the spell-checking was set to English (or to none).
Second thing, which is a question I’m having. I have some templates that I inherited, and clearly the person(s) involved had no idea about certain things (there are paragraphs set in a style where the only difference from normal is the colour and italics, and they have modified them to be roman and the same colour as Normal…) Anyway, documents that emanate from this group have Language: Asian (Korean) set on Normal and all dependent styles. I cannot seem to find where this is set; the Office Tools Language doesn’t find it, the lovely macro here doesn’t affect styles, the style itself doesn’t contain that language. (It has Korean, but that’s not the same description.)
The word “Asian” doesn’t even show up in the Registry, so I’m inclined to think it’s not Windows specifically.
Ideas?
by jhmcmullen August 2, 2013 at 10:56 pmHi jhmcmullen
Just guessing here…
I wonder if the ‘Asian (Korean)’ has come in from an earlier version of Word? You said you inherited these documents, so perhaps they were originally created in Word 2000/2003 or a Mac version where perhaps the language offerings were listed differently.
Have you saved this document to a newer version of Word? Can you then set the language either using the macro above or via the individual styles?
–Rhonda
by Rhonda August 3, 2013 at 6:49 am[…] have a macro for setting the language for all ‘ranges’ in a document, but I needed something to change the language settings for ALL styles in one command. After a bit […]
by Word: Macro to set the language for ALL styles | CyberText Newsletter September 21, 2018 at 3:51 pm[…] done on this one as yet—run the two macros I have for setting the language for most elements (https://cybertext.wordpress.com/2011/10/14/word-macro-to-set-the-language-for-most-eleme/) and for all styles […]
by Word: When spellcheck doesn’t work properly | CyberText Newsletter September 10, 2020 at 6:29 am