C#将每个循环的VBA转换为C#,但会产生编译错误(C# converting a VBA For Each loop to C# but gives compile error)

编程入门 行业动态 更新时间:2024-10-28 02:26:33
C#将每个循环的VBA转换为C#,但会产生编译错误(C# converting a VBA For Each loop to C# but gives compile error)

我正在将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 l

I 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;

更多推荐

本文发布于:2023-07-22 00:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   错误   VBA   converting   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!