0

Using LINQ, I am trying to iterate through the list objects in Lists collection of Microsoft WORD. But I'm getting the following error on the Where clause in the third line of my button click event below:

ERROR

List does not contain a definition for 'Where' and no accessible extension method 'Where' accepting a first argument of type 'List' could be found.

CODE

using System;
using MS_WORD = Microsoft.Office.Interop.Word;
using System.Linq;
....
private void BtnTest_Click(object sender, RoutedEventArgs e)
{
  Application application = new Application();
  Document document = application.Documents.Open("C:\\word.doc");

  MS_WORD.Lists oLsts = document.Lists.Where(n => n..Range.ListFormat.ListType == Word.WdListType.wdListSimpleNumbering);

  application.Quit();
}

References:

  1. ListFormat.ListType
  2. WdListType Enum
WalterDerHalter
  • 29
  • 2
  • 12
nam
  • 21,967
  • 37
  • 158
  • 332
  • It only implements `IEnumerable`. You could use `document.Lists.Cast().Where(...)` to do the filtering but then you'll need to figure out how to convert `IEnumerable` to `MS_WORD.Lists` (assuming you need that at all). – madreflection Sep 10 '19 at 00:02
  • @madreflection Your suggestion almost worked, now it gives the error `Lists does not contain a definition for 'Range' and and no accessible extension method 'Range' accepting a first argument of type 'Lists' could be found` – nam Sep 10 '19 at 00:21
  • You did `.Cast()` (with 's'). You want `.Cast()` (without 's'). – madreflection Sep 10 '19 at 00:26
  • @madreflection I had first tried without 's' and had gotten the error: `cannot implicitly convert type 'System.Collections.Generic.Ienumerable' to 'Microsoft.Office.Interop.Word.Lists'. An explicit conversion exists (are you missing a cast)` – nam Sep 10 '19 at 00:30
  • That's why I initially said *"you'll need to figure out how to convert `IEnumerable` to `MS_WORD.Lists` (assuming you need that at all)."* Thing is, you probably can't and you don't really need to. – madreflection Sep 10 '19 at 00:31
  • 1
    Instead, change the declaration to `List oLst = ...` and call `.ToList()` after the `Where`. You can iterate through that. – madreflection Sep 10 '19 at 00:32
  • @madreflection I guess you mean: `List oLsts = oDoc.Lists.Cast().Where(........).ToList()`. And this code works (thank you). And if I use this [link](https://stackoverflow.com/a/7757411/1232087) suggested by `@mjwills`, I can also use `IEnumerable oLsts = oDoc.Lists.Cast().Where(.....)` without using `.ToList()` at the end of `Where` clause. – nam Sep 11 '19 at 16:02
  • @mjwills Thank you. Your suggested [link](https://stackoverflow.com/a/7757411/1232087) helped me resolved the issue as follows: `IEnumerable oLsts = oDoc.Lists.Cast().Where(...);` Please also see my above comment. – nam Sep 11 '19 at 16:06
  • Yep, you got my meaning. I'm glad that worked (you're welcome). I was being pedantic on the "list" aspect of it (although I did say *"you don't really need to [convert to a list]"*). You can always count on mjwills for thoughtful and helpful suggestions. Thanks mjwills! – madreflection Sep 11 '19 at 16:17

0 Answers0