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

Hello!

Any ideas how to change the color palette entries in Excel using Win32::OLE? (running ActiveState Perl at the moment).

I tried things like:

$book->Colors->[$colourIndex] = excelRGB($rVal,$gVal,$bVal);

- or -

$book->Colors = $array;

Where excelRBG() packs three colours into a scalar or where $array points to an array of packed scalars. Apparently this is not correct as nothing seems to make the palette budge an inch.

Thanks!
Brett

Replies are listed 'Best First'.
Re: Excel Color Palette
by jmcnamara (Monsignor) on Aug 05, 2005 at 12:03 UTC

    It looks like the Colors array is resistant to change:
    #!/usr/bin/perl -wl use strict; use Cwd; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; my $application = Win32::OLE->new("Excel.Application"); my $workbook = $application->Workbooks->Add; my $worksheet = $workbook->Worksheets(1); print "Before: ", $workbook->Colors->[0]; print "Trying: ", $workbook->Colors->[0] = 255; print "After: ", $workbook->Colors->[0]; $workbook->SaveAs({ FileName => cwd() . '/win32ole.xls', FileFormat => xlNormal, }); $workbook->Close; __END__ Prints: Before: 0 Trying: 255 After: 0

    But I don't know why. Probably, Win32::OLE sets it as read-only but it is definitely writeable from VBA.

    --
    John.

Re: Excel Color Palette
by Courage (Parson) on Aug 05, 2005 at 12:15 UTC
    Here is my one-liner to get an idea:
    # ЭКСЕЛ -- ГАД И + ОКРУГЛЯЕТ &#10 +62;ВЕТ!!!!!!!!!!!!!!!! perl -MOLE -we "e->Selection->{Interior}->{Color}=245*256*256+255*256+ +255;print e->Selection->{Interior}->{Color}"
    Unreadable comment is my untranslatable from Russian comments on how excel rounds color numbers.

    Helper OLE.pm module is trivial:

    #simple helper for oneliner to get active word, excel #package OLE; use Win32::OLE qw(CP_UTF8); Win32::OLE->Option(CP=>CP_UTF8); sub e { return Win32::OLE->GetActiveObject('Excel.Application'); } sub w { return Win32::OLE->GetActiveObject('Word.Application'); } sub ao { return Win32::OLE->GetActiveObject(shift); } 1;

    Best regards,
    Courage, the Cowardly Dog


      That sets the cell colour.

      However, the OP wishes to change the palette colour, i.e. the definition of the colour associated with a workbook colour index.

      --
      John.

        John and Courage, Thanks for the replies. Yes, correct John I was hoping to change the definition of the colour palette itself. Your code example does seem to prove that it it will probably never work. Oh well, monochrome it is then. :-( Cheers! Brett