1
votes

I need to produce a large number of Powerpoint files with varying text (program of conference sessions). I try to do this with Perl and Win32::OLE. This works well with the exception of setting the color of the text I post. All I can set is the value for Red in RGB, but not the other colors. I use Powerpoint 2010. Also, I can change the color in Powerpoint via VBA.

Here is the code I use (commented with #- are some options that I tried and that did not work).

use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft PowerPoint';
$Win32::OLE::Warn = 2; # Throw Errors, I'll catch them

my $PptApp  = Win32::OLE->GetActiveObject('PowerPoint.Application')||   Win32::OLE->new('PowerPoint.Application', 'Quit'); 
$PptApp->{Visible} = 1;
my $Presentation = $PptApp->Presentations->Open({FileName=>'<input-filename.ppt>',ReadOnly=>1});

my $Slide = $Presentation->Slides(1);
$Slide->{Name} = "Slide1";

my $TextBox=$Slide->Shapes->AddTextbox({Orientation=>1, 
                                  Left=>25, 
                                  Top=>25,
                                  Width=>550,
                                  Height=>50,
});

$TextBox->TextFrame->TextRange->{Text} ="Big Ole Test";
$TextBox->TextFrame->TextRange->Font->{Color} = 255;

#-   $TextBox->TextFrame->TextRange->Font->{Color} => ({RGB=>(Red=>86, Green=>55, Blue=>201)});
   ## Black
#-   $TextBox->TextFrame->TextRange->Font->Color->RGB=>[255,255,255];
   ## Black
#-   $TextBox->TextFrame->TextRange->Font->Color => [255,255,255];
   ## Black
#-   $TextBox->TextFrame->TextRange->Font->Color->RGB => 'RGB(255,255,255)';
   ## Black
#-   $TextBox->TextFrame->TextRange->Font->{Color}->{RGB}=>[255,255,255];
   ## Black

$Presentation ->SaveAs('<output-filename.ppt>');
1

1 Answers

0
votes

You have to pass the value of the VBA RGB function. (Source) That's an integral value, not an array of integer values, or a string.

The value of RGB function can be calculated easily with some simple bit manipulation. The calculation for Red (r), Green (g) and Blue (b) components is:

(r) | (g << 8) | (b << 16)

Where | is the bitwise OR operator and << is the left shift operator. If you'd prefer to work without using bitwise operations, you could also use this calculation:

r + (g * 256) + (b * 65536)