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

hello,i have a problem,please help me.the code is as follows:

#!/usr/bin/perl -w use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; my $Excel; my $in_file_dgb = "e:/gen_ram_fifo_model/excel_list"; $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OL +E->new('Excel.Application', 'Quit'); $Excel->{DisplayAlerts} = 'False'; my $Book = $Excel->Workbooks->Open("$in_file_dgb/haha.xls"); my $worksheets_no = $Book->Worksheets->{Count}; undef $Book; undef $Excel;

the error message is : "Can't use an undefined value as a HASH reference at hehe.pl line 15 my $worksheets_no = $Book->Worksheets->{Count};". I think that the workbook is not opened,but i don't know why! I use windows xp and excel 2007. thanks!

Replies are listed 'Best First'.
Re: Excel: undef as hash ref
by Corion (Patriarch) on Mar 21, 2011 at 08:54 UTC
    $Excel->{DisplayAlerts} = 'False';

    ... does not do what you think it does. Most likely you want:

    $Excel->{DisplayAlerts} = 0;

    Also, if Excel doesn't tell you the reason, it's hard to find out the reason yourself. You can try to be more defensive:

    my $Book = $Excel->Workbooks->Open("$in_file_dgb/haha.xls") or die "Couldn't open '$in_file_dgb/haha.xls' via Excel.";

    Also consider using Spreadsheet::ParseExcel to see whether the Excel file actually is readable.