如何动态获取Word的语言ID。 [英] How to get the Language ID of Word dynamically.

查看:113
本文介绍了如何动态获取Word的语言ID。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望动态获取Word的语言ID并将其分配给Custom Dictionaries语言ID。当我知道Word正在使用的语言 Word.WdLanguageID.wdEnglishUS; 时,我可以这样做。但是如何动态地获得它。我尝试如下,但得到一个投射错误。我可以在VB6中轻松完成此操作,但需要c#中的解决方案。



无法隐式转换类型'Microsoft.Office.Core.MsoLanguageID 'to'Microsoft.Office.Interop.Word.WdLanguageID'





C#



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





VB6 - 作品



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

解决方案

VB6在强制执行变量类型方面是出了名的不好 - 它会跳过看不见的箍来尝试将一种类型的数据填充到一个变量中不同类型,通常是错误的。



C#对类型转换更加严格,99.9%的情况下这是一件好事。在这种特殊情况下,看起来这两个枚举具有相同的值,因此您只需要添加一个显式的强制转换:

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


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;


这篇关于如何动态获取Word的语言ID。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆