Reputation: 611
Does anyone know how to change the color of a selected range of text within a powerpoint add-in using C#?
Upvotes: 2
Views: 2630
Reputation: 123
If anyone is still looking for a solution:
I had the same problem. After spending some time figured out this way,
var paragraph1 = oTxtRange.Paragraphs(1);
paragraph1.Text = "Test ";
paragraph1.Font.Color.RGB = BGR(Color.Black);
var paragraph2 = oTxtRange.Paragraphs(2);
paragraph2.Text = "Application ";
paragraph2.Font.Color.RGB = BGR(Color.Green);
private int BGR(Color color)
{
// PowerPoint's color codes seem to be reversed (i.e., BGR) not RGB, so we have to produce the color in reverse
int iColor = (color.A << 24) | (color.B << 16) | (color.G << 8) | color.R;
return iColor;
}
I hope this helps!
Upvotes: 1
Reputation: 21
or just:
Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange.Font.Color.RGB = c.ToArgb();
where 'c' is your Color-element.
Upvotes: 2
Reputation: 999
Well, if you're using interop...
var app = new ApplicationClass();
app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
var myPresentation = app.Presentations.Open("c:\\test.pptx",
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue);
var slide1 = myPresentation.Slides[1];
var range = slide1.Shapes[1].TextFrame.TextRange;
range.Font.Color.RGB = -654262273;
And don't forget to
System.Runtime.InteropServices.Marshal.ReleaseComObject(<your com objects here>)
Upvotes: 1