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

2

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

c#
excel
interop
vsto
add-in
asked on Stack Overflow Jun 19, 2012 by woodykiddy • edited Mar 5, 2019 by halfer

1 Answer

7

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)
answered on Stack Overflow Jun 19, 2012 by Douglas

User contributions licensed under CC BY-SA 3.0