Search code examples
c#excelinteropvstoadd-in

Invalid index (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) when calling AddIns.Item[0]


I am receiving Invalid Index COM exception when calling Excel.AddIns.Item[0].

Excel.AddIns.Count works all right, which returns 4. But I just don't understand why Excel.AddIns.Item[0] fails to return the first Excel.AddIn object. The API doesn't seem to say too much about it either.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins.item


Solution

  • Most Office interop indexers are one-based, not zero-based (like the rest of the .NET Framework).

    You need to access your first element using Excel.AddIns.Item[1].

    Subsequently, when looping, use a for loop similar to the following:

    for (int i = 1; i <= Excel.AddIns.Count; ++i)