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

use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; $file = "t.xls"; my $Excel = Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open($file); my $Sheet1 = $Book->Worksheets(1); $Book->Windows(1)->{Zoom} = 75; $Sheet1->Columns->Font->{Size} = 12; my $Sheet2 = $Book->Worksheets(2); $Sheet2->Columns->{NumberFormat} = "#,##0.00"; $Book = $Excel->Workbooks->Close();
My question is:

After running the above perl program, sometimes View Zoom is changed to 75% in sheet #1 or sometimes View Zoom is changed to 75% in sheet #2. I'd like to know what I should write in order to the View Zoom in the sheet #2.

Thanks.

2007-02-07 Code tags added by Arunbear

Replies are listed 'Best First'.
Re: Set the sheet #2 for View Zoom to 75%
by Sagacity (Monk) on Jan 31, 2007 at 22:33 UTC

    After a little playing around with this code example, and Excel 2003, it appears that some of the methods seem to operate only on the active worksheet that was last used, or on-top (The view at 75%) is one of them. This leads me to believe that Excel needs a more in-depth pointer to the sheet you want to use the methods on. I am not overly familiar with how to manipulate these as you seem to need, but I did read that the structure available to access the sheets and their properties can be found in the Visual Basic Help.

    I did however include the code that I found from CPAN. Definitely, read the Win32::OLE documentation and use the Excel package example as it handles the closing of files even on abortion of the perl script. Otherwise, the excel file gets locked for editing. Wish I could do more :-(

    #!perl use Excel; my $file = "C:\\perl_proj\\book1.xls"; my $ex = Excel->new('Excel.Application', sub {$_[0]->Quit;}) or die "O +ops, cannot start Excel"; my $Book = $ex->Workbooks->Open($file); my $Sheet1 = $Book->Worksheets(1); $Book->Windows(1)->{Zoom} = 75; $Sheet1->Columns->Font->{Size} = 12; my $Sheet2 = $Book->Worksheets(2); $Sheet2->Columns->{NumberFormat} = "#,##0.00"; $Book = $ex->Workbooks->Close();
    The Excel Module from the CPAN page Win32::OLE
    package Excel; use Win32::OLE; sub new { my $s = {}; if ($s->{Ex} = Win32::OLE->new('Excel.Application')) { return bless $s, shift; } return undef; } sub DESTROY { my $s = shift; if (exists $s->{Ex}) { print "# closing connection\n"; $s->{Ex}->Quit; return undef; } } sub AUTOLOAD { my $s = shift; $AUTOLOAD =~ s/^.*:://; $s->{Ex}->$AUTOLOAD(@_); } 1;
Re: Set the sheet #2 for View Zoom to 75%
by Sagacity (Monk) on Jan 31, 2007 at 20:57 UTC

    I have reposted this using tags

    use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; $file = "t.xls"; my $Excel = Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open($file); my $Sheet1 = $Book->Worksheets(1); $Book->Windows(1)->{Zoom} = 75; $Sheet1->Columns->Font->{Size} = 12; my $Sheet2 = $Book->Worksheets(2); $Sheet2->Columns->{NumberFormat} = "#,##0.00"; $Book = $Excel->Workbooks->Close();

    My question is: After running the above perl program, sometimes View Zoom is changed to 75% in sheet #1 or sometimes View Zoom is changed to 75% in sheet #2. I'd like to know what I should write in order to the View Zoom in the sheet #2. Thanks.

    2007-01-31 Considered by blue_cowdawg: delete: essentially a duplicate of OP with code tags added.
    2007-02-02 Unconsidered by Arunbear: enough keep votes (Keep: 8, Edit: 0, Reap: 13)