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

Any idea on how to format a whole Excel worksheet to use Arial font, font size 8 and AutoFit?

I tried the following lines, they would not work:
$Sheet->{Font} = "Arial"; $Sheet->{Size} = 8; $Sheet->Range($range)->Columns->AutoFit;

Any web link from which I can learn more about the excel's formatting methods/properties under Win32::0le?

Replies are listed 'Best First'.
Re: Win32::OLE formatting Excel Worksheet
by guha (Priest) on May 10, 2004 at 15:24 UTC

    There are two utilities that are invaluable when using Win32::OLE.

    1. The macro recording facility in Excel/Word/insert the OLE engine of your choice. It is pretty straightforward to translate VBA to Perl.

    2. Our own brother jand's OLE browser which is included at least in the ActiveState distribution. This utility can't be praised enough.

    As for your question I think it will be an nice exercise to answer it yourself using the two tools above.

    Welcome back if all else fails!

      Another tool I've used extensively is the object browser from within Excel itself. Press Alt-F11 to get to the VB editor, then F2 to get to the browser itself.
      Thanks all for the information.

      I do not find OLE browser, however, I am getting hands on the use of Excel's Object Browser for those method/property access.
      $Book2->Styles("Normal")->{Font}->{Name} = "Arial";

      Thanks again for example and info.

        On my box I can invoke the OLE Browser either via Start-Program-ActiveState-Ole Browser or by clicking the link in the TOC of the html-docs.

        The link is in fact

        file:///C:/Perl/html/OLE-Browser/Browser.html
        but I don't know how that maps to your setup.

Re: Win32::OLE formatting Excel Worksheet
by jmcnamara (Monsignor) on May 10, 2004 at 18:06 UTC

    The best way to format all text in a Excel workbook is to change the Normal Style setting. Here is an example:
    #!/usr/bin/perl -w 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); $workbook->Styles("Normal")->{Font}->{Size} = 8; $workbook->Styles("Normal")->{Font}->{Bold} = 1; # Write some text. $worksheet->Cells(1,1)->{Value} = "Hello World"; my $dir = cwd(); $workbook->SaveAs({FileName => $dir . '/win32ole.xls'}); $workbook->Close; __END__
    Note that this is applied to all worksheets in the workbook.

    --
    John.

Re: Win32::OLE formatting Excel Worksheet
by Grygonos (Chaplain) on May 10, 2004 at 17:31 UTC

    Can't agree more.. just read the docs... I can do just about anything I want to in Excel with OLE now that I gave them a good read. Honestly the Microsoft doc for Excel is good (once you understand their style). True its all VBA code, but its the same object model (duh!) so it should make sense if you read it like pseudo-code

    Good luck with your problem. It is an easy one to fix and will be much more rewarding when you figure it out yourself. It will get you familiarized with the object browser in Excel also. This is a good and simple start for learning how to do more than simply place values in a cell

    If you have more questions once you have read the docs or don't understand something.. please update us...