Powerpoint interop with C# - trouble accessing slide shapes RRS feed

  • Question

  • I am using Visual Studio Community and have added a reference to the Microsoft.Office.Interop.PowerPoint COM library to my project. I am not developing a VSTO application but want to create and modify a PowerPoint file suing data from my application. I am able to successfully create a new presentation and add a slide to it by copying the slide from another PowerPoint file. That slide contains some Shapes that I need to modify. I have tried to access the shapes one at a time with various methods. In what follows PPT = Microsoft.Office.Interop.PowerPoint

      I've tried

                System.Collections.IEnumerator e = theSlide.Shapes.GetEnumerator();
                while (e.MoveNext())
                    PPT.Shape aShape = (PPT.Shape)e.Current;

    I get an error that I cannot cast a _com object to PPT.Shape object on the e.Current line. I've tried

                for (int i = 1; i <= theSlide.Shapes.Count; i++)
                    PPT.Shape aShape = theSlide.Shapes[i];
    where I get an 'object not found' error on the reference to Shapes[i]. When I use the debugger to look at Slides, I see the Count=2, but there does not appear to be any actual collection associated with it. Also, the Item attribute is not visible, even through it is letting me use the [] shorthand.

    I have also tried 'foreach' where I get a cast error like the use of e enumerator.

    • Edited by Blane245 Thursday, December 19, 2019 3:58 PM typo
    Wednesday, December 18, 2019 2:31 PM