Close

August 16, 2010

PHP RGB Function for Excel or Word Document COM Manipulation

Recently I had a project that needed to create and format a word document, based on data from a MySQL database. While the solution was on an MS platform that had Word available, I could only use PHP for the scripting language as it was a language that was used by the rest of the team. The system would call the Word com object, create a new word document and formatted a report, using the “word.application” COM object.

One issue I ran across was that that coloring a font object in any VBA Macro script often times used a simple built-in function called “RGB”. I translated a PERL version below that seems to work quite well in PHP. Hope this helps someone else. This can also be used in formated RGB values in Excel document manipulations.

function phpRGB ($r,$g,$b){ 
	return $r + (256*$g) + (65536*$b);
}
 
//example usage:
    $wobj->Selection->Font->Italic = 1;
    $wobj->Selection->Font->Color = phpRGB(153,204,255);