CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Win32 Programming Help:
Problem using scalar vs number in Win32::OLE



Sep 21, 2000, 7:25 PM

Post #1 of 3 (7972 views)
Problem using scalar vs number in Win32::OLE Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my $num = 1;
my $sheet = $self->{book}->Worksheets($num) or die Win32::OLE->LastError();</pre><HR></BLOCKQUOTE>

doesn't work.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my $sheet = $self->{book}->Worksheets(1) or die Win32::OLE->LastError();</pre><HR></BLOCKQUOTE>

does. It only works when a hard number is inserted, not a variable containing a number, even if it's the same number (WEIRD!!!).

Does anyone see why/how this doesn't work? The error is:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

Win32::OLE(0.14) error 0x8002000b: "Invalid index"
in METHOD/PROPERTYGET "Worksheets" at ...</pre><HR></BLOCKQUOTE>


Sep 21, 2000, 10:07 PM

Post #2 of 3 (7972 views)
Re: Problem using scalar vs number in Win32::OLE [In reply to] Can't Post

I think the problem is the referencing/dereferencing. Here is an example that works ok using your requested methodology.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use Win32::OLE;
# use existing instance if Excel is already running
eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
die "Excel not installed" if $@;
unless (defined $ex) {
$ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Excel";
# get a new workbook
$book = $ex->Workbooks->Add;
# write to a particular cell
$num = 1;
$sheet = $book->Worksheets($num);
$sheet->Cells(1,1)->{Value} = "foo";
# write a 2 rows by 3 columns range
$sheet->Range("A8:C9")->{Value} = [[ undef, 'Xyzzy', 'Plugh' ],
[ 42, 'Perl', 3.1415 ]];
# print "XyzzyPerl"
$array = $sheet->Range("A8:C9")->{Value};
for (@$array) {
for (@$_) {
print defined($_) ? "$_|" : "<undef>|";
print "\n";
# save and exit
$book->SaveAs( glob "./test.xls" );
undef $book;
undef $ex;




Sep 22, 2000, 10:10 PM

Post #3 of 3 (7972 views)
Re: Problem using scalar vs number in Win32::OLE [In reply to] Can't Post

Actually, I just figured it out... For some odd reason, I needed to int() the number (even though it was a plain ole number)

my $sheet = $self->{book}->Worksheets(int($num)) or die Win32::OLE->LastError();

Thanks for the help!


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives