简体   繁体   中英

How to get the Language ID of Word dynamically

I want to get the language id of Word dynamically and assign it to Custom Dictionaries language id. I can do this when I know the language the Word is using Word.WdLanguageID.wdEnglishUS ;. But how to get this dynamically. I tried as below but get a casting error. I can do this easily in VB6 but need a solution in c#.

Cannot implicitly convert type 'Microsoft.Office.Core.MsoLanguageID' to 'Microsoft.Office.Interop.Word.WdLanguageID'

C#

oCustDict.LanguageSpecific = true;
oCustDict.LanguageID = WordApp.Language;

VB6 - Works

Dim lCurrentLanguage As Long
CurrentLanguage = WordApp.Language
oCustDict.LanguageSpecific = True
oCustDict.LanguageID = lCurrentLanguage

VB6 was notoriously bad at enforcing variable types - it would jump through invisible hoops to try to stuff data of one type into a variable of a different type, often incorrectly.

C# is much stricter about type conversions, and in 99.9% of cases that's a good thing. In this particular case, it looks like the two enums have the same values, so you just need to add an explicit cast:

oCustDict.LanguageID = (Microsoft.Office.Interop.Word.WdLanguageID)WordApp.Language;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM