I am having trouble modifying a currently active excel window with Win32::OLE.

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.


In reply to How do I change the activesheet of an already-open excel spreadsheet using perl? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.