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.