1
votes

I am writing a module for a VB.NET web page, which takes values from a database and inserts them into Office documents. Using the Open XML SDK in Visual Studio, I have managed to modify both Excel and PowerPoint files, but I have an issue with the color in PowerPoint's charts:

How can I change the color of a PowerPoint chart using the Open XML SDK, without also making the color separate itself from the theme?

Details below.

After adding my values to the worksheet behind the chart, and updating the visual lines/bars, all of the series appear to be using the same color. Therefore I also need to set the color of the series in the chart. I currently do this with the following code:

Dim barChart1 As DocumentFormat.OpenXml.Drawing.Charts.BarChart = chartPart.ChartSpace.Descendants(Of BarChart).First
Dim barChartSeries = barChart1.Descendants(Of BarChartSeries).ToList
barChartSeries(0).ChartShapeProperties.Descendants(Of SolidFill).First.SchemeColor = New SchemeColor() With 
{ .Val = DocumentFormat.OpenXml.Drawing.SchemeColorValues.Accent4}

Basically, I set the first bar in the chart to use Accent4 as its color. Accent1, 2, 3 and so on correspond to the colors in the color palette of the PowerPoint presentation's theme. After this code runs, the first bar of the chart will be using the fourth color of the current palette.

The problem I'm facing is that after the color is set with code, the color value appears to be saved separately from the actual theme colors.

If I open the PowerPoint file and change the theme, all colors change except the ones I modified. In this example, the first bar of the chart would keep the fourth color for the previous theme, instead of using the fourth color of the new theme as it's supposed to.

This becomes a problem because my company's clients are supposed to be able to change the theme to one that suits them better, but they can't do that easily if some of the colors stick.

I'm using the Accent colors because the alternative seems to be setting an actual RGB color value, which leads to the same problem. I have tried the PowerTools plugin by Eric White, which simplifies the process of updating the charts, but it does not appear to solve the color problem. I may be using it wrong, but I've been unable to find specific info on how to debug color issues.

Thank you in advance for any help.

1

1 Answers

1
votes

I'm not sure if this is true for VB.NET and OpenXML, but in the PPT object model, Scheme colors are still supported to maintain compatibility with pre-2007 versions of PowerPoint. New versions use color themes, not schemes. (yeah, I know. Welcome to the BizarroWorld that is PowerPoint).

In VBA, you'd use something like this to set the fill color of the shape referred to by oSh to Accent Color 2 in the current color THEME.

oSh.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent2