How can I identify the display language (i.e. toolbars/menus) used by MS Office in Visual Basic?

carbontracking picture carbontracking · Jul 30, 2012 · Viewed 10.1k times · Source

I have a macro that generates an MS Word report from an MS Excel spreadsheet. The styles I use in the report are coded in English in the Macro.

Some of my team have their MS Office display language set to France (this is not weird, I work in France). My macro thus doesn't work as the style title (in English) is used to set the styles. e.g. I'm telling MS Word to use style "List Bullet 1" whereas the corresponding style in MS Word is "Liste à puce 1" so no match is found.

So my question is, how can I detect the MS Office display language and then set the style value in the correct language ?

Another question is, is there another way of referencing styles besides the title ?

Best regards,

Colm

Answer

DotNetDeveloper picture DotNetDeveloper · Jul 30, 2012

Colmatairbus,

GSerg's link provides excellent reference to how Microsoft VBA deals with language settings.

Using VBA, you can reference the language in word for a variety of purposes. For instance, you can determine the current language, store it in a variable, and display it in a messagebox:

    Sub LanguageMessageBox()

        CurrentLanguage = Selection.LanguageID
        MsgBox (CurrentLanguage)

    End Sub

You can find the languageID list here: http://msdn.microsoft.com/en-us/library/bb213877(v=office.12).aspx

You can also simply reference the language itself, especially if you do not want to look the numbers or use the numbers in some algorithmic way:

Sub LanguageMessageBox()

    CurrentLanguage = Selection.LanguageID
    MsgBox (Languages(CurrentLanguage))

End Sub

As for changing the language settings, you can easily change the language settings by referencing the languageIDs:

Sub ChangeLanguage()

    ' 1033 is wdEnglishUS
    ' 1034 is wdSpanish        
    ' 1036 is wdFrench

    If Selection.LanguageID = 1033 Then
        Selection.LanguageID = 1034
        Else
        Selection.LanguageID = 1036
    End If

'Set the grammar dictionary for error checking purposes
Set dicGrammar = Languages(Selection.LanguageID).ActiveGrammarDictionary

End Sub