Search code examples
c#comobject

Unable to case COM object of type


I'm getting a strange error when I'm trying to do a foreach through shapes on a page using Word interop

My code is as follows

    foreach (word.Shapes p in pages[3].Application.ActiveDocument.Shapes )
    {
        p.SelectAll();
    }

The exception I get for some reason is this and I've no idea what I should do to fix this.

{"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Word.Shapes'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{0002099F-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."}


Solution

  • Shapes is the collection type, but in order to iterate the shapes, you have to use Shape as type for each object.