Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Win32::OLE, Word, and BackgroundPatternColor

by g0n (Priest)
on Mar 29, 2006 at 15:59 UTC ( [id://539992]=perlquestion: print w/replies, xml ) Need Help??

g0n has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I'm programatically generating a word document which contains tables. I need to set the top row cells of certain tables to have a background colour of 'gray30'. By recording and examining macros I got as far as BackgroundPatternColor, but it only seems to do anything when I supply a number:

$table->Cell(1,1)->Range->{Shading}->{BackgroundPatternColor} = 254;

Supersearching and googling hasn't got me any further towards what the number codes are, so I decided to find out empirically:

use strict; use warnings; #create a word doc with all the background colour codes use Win32::OLE; my $word = CreateObject Win32::OLE 'Word.Application' or die $!; $word->{'Visible'} = 1; my $document = $word->Documents->Add; my $colour=0; my $selection = $word->Selection; my $table = $document->Tables->Add($selection->Range,2000,1); for (0..2000) { $table->Cell($colour,1)->Range->{Text} = "Colour $colour"; $table->Cell($colour,1)->Range->{Shading}->{BackgroundPatternColor +} = $colour; $selection->TypeParagraph; $selection->TypeParagraph; $selection->MoveDown({count=>1}); $colour++; }

This little script produces a word doc, with a very large table in it, each cell of which is a different colour. When I run it, I get a range of colour from 'red so dark as to be black' up to carmine at 255, then it starts at 'red so dark as to be black' again.

Can someone point me in the right direction?

TIA

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
John Brunner, "The Shockwave Rider".

Can you spare 2 minutes to help with my research? If so, please click here

Replies are listed 'Best First'.
Re: Win32::OLE, Word, and BackgroundPatternColor
by jdporter (Paladin) on Mar 29, 2006 at 16:17 UTC

    It's just like HTML colors: three bytes, one per color. However, the order of the bytes is BGR. I.e. 0xFF0000 is fully saturated blue, and 0x0000FF is fully saturated red.

    If that gives you more range than you want to work with, you could consider setting the BackgroundPatternColorIndex property instead. The range of possible values is:

    1 Black 2 Blue 3 Turquoise 4 BrightGreen 5 Pink 6 Red 7 Yellow 8 White 9 DarkBlue 10 Teal 11 Green 12 Violet 13 DarkRed 14 DarkYellow 15 Gray50 16 Gray25

    If you'd like to know how I discovered all that, launch the OLE Browser that comes with ActiveState Perl. (Go to the main page (index.html) of the documentation set installed with AS Perl; look for the first link under the Windows Specific header.) Note - The OLE Browser requires IE to run properly, as it uses an ActiveX control.

    Now, in the second frame, find and click "Microsoft Word 11.0 Object Library" (or whatever version is on your system). Now find "Shading" in the third frame. You'll also be looking for WdColor and WdColorIndex in the third frame. (Btw - I also recommend checking "Group elements by type" in the top frame.) HTH, HAND.

    We're building the house of the future together.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://539992]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (None)
    As of 2024-04-25 01:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found