Search code examples
c#powerpointopenxml

Fill a specific Shape - ShapeStyle with color C# PowerPoint OpenXML


I would like to change the fill color of a few shapes in a slide.

I found this tutorial on msdn, but it only mention a very specific scenario (first shape of first slide, in a presentation where there is only one shape) => Link To The MSDN Tutorial

So, maybe I don't really understand the whole thing, but it seems to me that when you try to access the ShapeTree Elements dynamically, you get a "nullReference exception" or a "not define as an instance of an object", when trying to access the ShapeStyle property (even with the FirstChild() example actually).

So I tried to access it as follows, by itering through the Elements, but no success neither.

 ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;
            for (var i = 0; i <= 30; i++)
            {
                try
                {
                    DRAW.FillReference fillRef = tree.ElementAt(i).GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>().ShapeStyle.FillReference;


                    fillRef.SchemeColor = new DRAW.SchemeColor();
                    fillRef.SchemeColor.Val = DRAW.SchemeColorValues.Accent6;

                }
                catch (Exception e)
                {
                //Null reference Excpetion on the fillRef initialization, does the same if I cast with DocumentFormat.OpenXml.Drawing.Shape        
                }

           }

My goal is really simple, I'd like to change the fill color of a shape, by identifying it's innerText.

Thanks in advance !

Cheers


Solution

  • Problematic has been answered here -> https://social.msdn.microsoft.com/Forums/en-US/292dc670-7a9c-4df5-aa9f-d221b73b54d9/fill-a-specific-shape-shapestyle-with-color-c-powerpoint-openxml?forum=oxmlsdk