Why am I getting an exception when I try to set the value of a cell in Excel using Perl's Win32::OLE?

0

I am getting the error Win32::OLE<0.1709> error 0x80020009: "Exception occurred" in PROPERTYPUT "Value" at line 109.

The code in is Perl.

foreach my $ref_array1 (@$array1) {     # loop through the array
 foreach my $col1 (@$ref_array1) {     
   foreach my $ref_array2 (@$array2) {     # loop through the array   
     foreach my $col2 (@$ref_array2) {      
       if ($col1 eq $col2)
        {

             this is line 109: **$worksheet1->Cells($j,1)->{'Value'} = $col1;**

             $j++;

Any kind of help is appreciated. Thankyou

perl
excel
win32ole
asked on Stack Overflow Jul 26, 2010 by user402705 • edited Jul 26, 2010 by Sinan Ünür

1 Answer

3

The following incomplete example works (i.e., it puts 5 in cell A1):

#!/usr/bin/perl

use strict; use warnings;

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;

my $excel = get_excel();
$excel->{Visible} = 1;

my $book = $excel->Workbooks->Add;
my $sheet = $book->Worksheets->Add;
$sheet->{Name} = 'Perl Win32-OLE Example';

my $range = $sheet->Cells(1,1);
$range->{Value} = 5;
$range->AutoFormat;

sub get_excel {
    my $excel;

    unless ( eval {
            $excel = Win32::OLE->GetActiveObject('Excel.Application')
    }) {
        die $@, "\n";
    }

    unless(defined $excel) {
        $excel = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit })
            or die "Oops, cannot start Excel: ",
                   Win32::OLE->LastError, "\n";
    }
    return $excel;
}

See also my Perl Win32::OLE example.

answered on Stack Overflow Jul 26, 2010 by Sinan Ünür

User contributions licensed under CC BY-SA 3.0