Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I want to be able to do the following:
1) Open an excel spreadsheet called 'SummarySheet.xls' manually.
2) THEN run a perl script that manipulates that spreadsheet.
This is no problem if I confine myself to modifying only the active sheet. This code, for instance, works fine:
use Win32::OLE::Variant; use Win32::OLE qw(in with); use Win32::OLE::Const "Microsoft Excel"; use strict; my $excel = Win32::OLE->GetActiveObject('Excel.Application')||die "no" +; $excel -> Windows("SummarySheet.xls") -> Activate; my $workbook = $excel -> Activewindow; my $sheet = $workbook -> Activesheet; $sheet -> Cells(1,1) -> {Value} = "This works fine.";
However, when I attempt to choose the sheet to manipulate, I get an error saying that the sheet isn't defined:
use Win32::OLE::Variant; use Win32::OLE qw(in with); use Win32::OLE::Const "Microsoft Excel"; use strict; my $excel = Win32::OLE->GetActiveObject('Excel.Application')||die "no" +; $excel -> Windows("SummarySheet.xls") -> Activate; my $workbook = $excel -> Activewindow; my $sheet = $workbook -> Sheets(2); $sheet -> Cells(1,1) -> {Value} = "This does not work fine.";
I would assume that somehow I'm using the wrong syntax to define the sheet, but if I create a new workbook (instead of selecting the existing one), it works fine:
my $excel = Win32::OLE->GetActiveObject('Excel.Application')||die "cou +ldnt get active one"; my $workbook = $excel -> Workbooks -> Add; $workbook->Activate; my $sheet = $workbook -> Sheets(2); $sheet -> Cells(1,1) -> {Value} = "This works well";
To sum up, I manually open a spreadsheet called "SummarySheet.xls", and I am trying to work things so I can then run a perl script that will modify multiple sheets of that spreadsheet. Thanks in advance for your help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I change the activesheet of an already-open excel spreadsheet using perl?
by Anonymous Monk on May 01, 2009 at 14:06 UTC |