简体   繁体   English

如何动态获取Word的语言ID

[英]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. 我想动态获取Word的语言ID,并将其分配给“自定义词典”语言ID。 I can do this when I know the language the Word is using Word.WdLanguageID.wdEnglishUS ;. 当我知道Word使用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#. 我可以在VB6中轻松地做到这一点,但需要使用C#解决方案。

Cannot implicitly convert type 'Microsoft.Office.Core.MsoLanguageID' to 'Microsoft.Office.Interop.Word.WdLanguageID' 无法将类型“ Microsoft.Office.Core.MsoLanguIDID”隐式转换为“ Microsoft.Office.Interop.Word.WdLanguageID”

C# C#

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

VB6 - Works VB6-工作

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. 众所周知,VB6在强制变量类型方面很差-它会跳过看不见的圈,试图将一种类型的数据填充到另一种类型的变量中,这通常是错误的。

C# is much stricter about type conversions, and in 99.9% of cases that's a good thing. C#对于类型转换要严格得多,在99.9%的情况下,这是一件好事。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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