我正在将Word VBA宏转换为C#中的插件。
到目前为止,我已经成功地重构了C#中的所有语句,方法和属性,但是这个让我很难过:
For Each l In Application.Languages If InStr(LCase(l.NameLocal), LCase(Language)) > 0 Then Selection.LanguageID = l.ID Exit For End If Next l我在C#中将上面的内容转换如下:
using Microsoft.Office; using Microsoft.Office.Interop; using Word = Microsoft.Office.Interop.Word; Word.Application oWord = new Word.Application(); Word.Document oWordDoc = new Word.Document(); var Selection = oWordDoc.ActiveWindow.Selection; string strTgtLanguage = "Hungarian"; foreach (var item in oWord.Application.Languages) { if (item.NameLocal.IndexOf(strTgtLanguage)>-1) //The error is ---^ here on 'NameLocal'. { Selection.LanguageID = item.ID //And here on 'ID' -----------------------^ break; } }两个实例的编译器错误是:
'object'不包含'NameLocal'的定义,并且没有扩展方法'NameLocal'接受类型'object'的第一个参数可以找到(你是否缺少using指令或汇编引用?)
我在这做错了什么? 我认为foreach语句正确地声明了集合中的object 。
提前致谢。
I am converting a Word VBA macro to a plugin in C#.
I have so far successfully refactored all statements, methods and properties in C#, but this one gives me a hard time:
For Each l In Application.Languages If InStr(LCase(l.NameLocal), LCase(Language)) > 0 Then Selection.LanguageID = l.ID Exit For End If Next lI have converted the above in C# as follows:
using Microsoft.Office; using Microsoft.Office.Interop; using Word = Microsoft.Office.Interop.Word; Word.Application oWord = new Word.Application(); Word.Document oWordDoc = new Word.Document(); var Selection = oWordDoc.ActiveWindow.Selection; string strTgtLanguage = "Hungarian"; foreach (var item in oWord.Application.Languages) { if (item.NameLocal.IndexOf(strTgtLanguage)>-1) //The error is ---^ here on 'NameLocal'. { Selection.LanguageID = item.ID //And here on 'ID' -----------------------^ break; } }The compiler error for both instances is:
'object' does not contain a definition for 'NameLocal' and no extension method 'NameLocal' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
What is that I am doing wrong here? I thought the foreach statement properly declares the object from the collection.
Thanks in advance.
最满意答案
用类型名称替换var item 。
foreach (var item in oWord.Application.Languages)替换为foreach (Word.Language item in oWord.Languages)
在第一种情况下使用var ,变量item的类型为System.Object 。
完成for-each使用Linq可能如下所示:
foreach (Word.Language item in oWord.Languages.Cast<Word.Language>() .Where(item => item.NameLocal.IndexOf(strTgtLanguage, StringComparison.Ordinal) > -1)) { selection.LanguageID = item.ID; break; }或者for-each可以替换为例如FirstOrDefault :
Word.Language hungarian = oWord.Languages.Cast<Word.Language>() .FirstOrDefault(item => item.NameLocal.IndexOf(strTgtLanguage, StringComparison.Ordinal) > -1); if (hungarian != null) selection.LanguageID = hungarian.ID;Replace var item with type name.
foreach (var item in oWord.Application.Languages) replace with foreach (Word.Language item in oWord.Languages)
In the first case with var the variable item is of type System.Object.
Complete for-each using Linq could look like this:
foreach (Word.Language item in oWord.Languages.Cast<Word.Language>() .Where(item => item.NameLocal.IndexOf(strTgtLanguage, StringComparison.Ordinal) > -1)) { selection.LanguageID = item.ID; break; }Or the for-each can be replace with e.g. FirstOrDefault:
Word.Language hungarian = oWord.Languages.Cast<Word.Language>() .FirstOrDefault(item => item.NameLocal.IndexOf(strTgtLanguage, StringComparison.Ordinal) > -1); if (hungarian != null) selection.LanguageID = hungarian.ID;更多推荐
发布评论