Unable to case COM object of type

0

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))."}

c#
comobject
asked on Stack Overflow Jun 8, 2017 by Craig Gallagher • edited Jun 8, 2017 by SharpGobi

1 Answer

4

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

answered on Stack Overflow Jun 8, 2017 by grek40

User contributions licensed under CC BY-SA 3.0