Creating PowerPoint presentations in C# Visual Studio, I needed to create "colorFormat" object but I realize there is not constructor.
I have this code example in "C:\myPowerPoint\" for C# Windows Forms .NET
I have added from Reference Manager>COM>Type Libraries the assembly reference "Microsoft Office 16.0 Object Library" (Version 2.8), or also from NuGet: "Microsoft.Office.Interop.PowerPoint" (Version 15.0.4420.1018) added to my project...
I have created Form1, and button1:
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using State = Microsoft.Office.Core.MsoTriState;
using System;
using System.Windows.Forms;
namespace myPowerPoint
{
public partial class Form1 : Form
{
Microsoft.Office.Interop.PowerPoint.Application pptApplication = null;
PowerPoint.Presentation ppt = null;
PowerPoint.Slides slides = null;
PowerPoint.Slide slide = null;
PowerPoint.ColorFormat colorFormat = null;
readonly float u = 72.0f; // Inches
int k = 0; // Loop index
public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
// Create a temporary PowerPoint Application to interact with
pptApplication = new PowerPoint.Application();
ppt = pptApplication.Presentations.Add(State.msoFalse);
ppt.Slides.Application.Caption = "My First PowerPoint";
slides = ppt.Slides; // pptApplication.Presentations.Slides
// First Slide - Blank
slide = slides.AddSlide(1, ppt.SlideMaster.CustomLayouts[PowerPoint.PpSlideLayout.ppLayoutText]);
slide.Layout = PowerPoint.PpSlideLayout.ppLayoutBlank;
// First Shape - FlowchartTerminator
slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeFlowchartTerminator, 6.61f * u, 4.57f * u, 0.77f * u, 0.23f * u);
int i = slide.Shapes.Count;
colorFormat = applyMyTemplate(slide.Shapes[i].Fill.ForeColor);
slide.Shapes[i].TextFrame.TextRange.Font.Name = "Arial Narrow";
slide.Shapes[i].TextFrame.TextRange.Font.Size = 5.0f;
slide.Shapes[i].TextFrame.VerticalAnchor = Office.MsoVerticalAnchor.msoAnchorMiddle;
slide.Shapes[i].TextFrame.HorizontalAnchor = Office.MsoHorizontalAnchor.msoAnchorCenter;
slide.Shapes[i].TextFrame.TextRange.Text = "Box";
slide.Shapes[i].Top = 4.57f * u;
slide.Shapes[i].Width = 0.77f * u;
slide.Shapes[i].Fill.ForeColor = colorFormat; // Here
slide.Shapes[i].Line.ForeColor = colorFormat; // Here2
slide.Shapes[i].Shadow.ForeColor = colorFormat; // Here3
slide.Shapes[i].Height = 0.23f * u;
slide.Shapes[i].Left = 6.61f * u;
// Save and close PowerPoint
string path = @"C:\myPowerPoint\";
string file = "Presentation_" + System.DateTime.Now.ToUniversalTime() + ".ppt";
file = file.Replace(":", "_").Replace("/", "_").Replace(" ", "_");
if (System.IO.File.Exists(path + file)) System.IO.File.Delete(path + file);
ppt.SaveAs(path + file, PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Office.MsoTriState.msoCTrue);
ppt.Close();
}
private PowerPoint.ColorFormat applyMyTemplate(PowerPoint.ColorFormat colorFormat)
{
// Do some processing... Just an example, ignore it
k++;
if (k == 1) { colorFormat.SchemeColor = PowerPoint.PpColorSchemeIndex.ppAccent1; }
if (k == 2) { colorFormat.SchemeColor = PowerPoint.PpColorSchemeIndex.ppAccent2; }
if (k == 3) { colorFormat.SchemeColor = PowerPoint.PpColorSchemeIndex.ppAccent3; k = 0; }
return colorFormat;
}
}
}
Exception due to colorFormat need to be defined
So I need to know how to create colorFormat.
The question is resolved when "colorFormat" is properly defined in "slide.Shapes[i].Fill.ForeColor = colorFormat;" so the code will generate a Power Point file. Such presentation will show the backcolor in "Box" defined by colorFormat.
Edited: Nov. 24, 2024. I have replaced BackColor by ForeColor as:
slide.Shapes[i].Fill.ForeColor = colorFormat; // Here
Edited: Nov. 26, 2024. I need to reformulate the question since looks like I am asking about RGB color but, as Victor pointed out below, I assumed ColorFormat was a Class but an Interface. So, I have updated the code where I want to apply my template. Is it the right solution to the question? Please, the applyMyTemplate() function is just a functional example and it is not relevant to the question, do not try to optimize.
Actually the ForeColor
is the COM interface. Therefore, you can use methods and properties it provides.
For example, to definition the shape fill color:
// The code based on the fragment from the question above
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
...
var shape = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeFlowchartTerminator, 6.61f * u, 4.57f * u, 0.77f * u, 0.23f * u);
shape.Fill.ForeColor.RGB = System.Drawing.Color.FromArgb(0, 127, 0).ToArgb();
shape.Height = 0.23f * u;
shape.Left = 6.61f * u;
...
Any another method returning the System.Drawing.Color
type might be used to obtain required color. For example:
shape.Fill.ForeColor.RGB = System.Drawing.Color.FromName("BurlyWood").ToArgb();