Howdy - I have strange Perl problem. I get zipped XLS file generated from a database. I wrote some Win32::OLE code to unzip file and extract data however I keep running into same issue.
If file was just extracted and never manually opened and OLE tries to get hold of Excel Workbook object the procedure fails.
If file was manually opened and then saved then OLE works as expected.
I use fairly standard way of extracting data:
unless (defined $excel) { eval { $excel = Win32::OLE->GetActiveObject('Excel.Application') }; die "Excel not installed" if $@; } unless (defined $excel) { $excel = Win32::OLE->new('Excel.Application', 'Quit') or die "Oops, cannot start Excel"; } #to avoid excessive dialogs when saving in non-Excel format $excel->{DisplayAlerts} = 0; #$excel->Workbooks->Open({ FileName => $fullname_input_file }) #|| print STDERR "didnt open the file $!\n"; # get a new workbook my $wbook; if($wbook = Win32::OLE->GetObject($fullname_input_file)){;} else { $wbook = $excel->{Workbooks}->{"$fullname_input_file"}; if($wbook->Activate()){;} else { print STDERR "Could not open the file $fullname_input_file +: $!\n"; exit; } }
But if file was not opened manually I get:
Win32::OLE(0.1709) error 0x800401e6: "Bad extension for file" at oleli +b.pl line 1286. eval {...} called at olelib.pl line 1286 main::save_excel_file_as_tab_delimited_MS_API_WSC('test2.xls', + '__read_in_excel_file_into_array.txt', 'ANNOUNCEMENT') called at ole +lib.pl line 411 main::save_excel_file_as_tab_delimited('test2.xls', '__read_in +_excel_file_into_array.txt', 'ANNOUNCEMENT') called at olelib.pl line + 1970 main::read_in_excel_file_into_array_WSC('test2.xls', 'ARRAY(0x +2b4a334)', 'ANNOUNCEMENT') called at olelib.pl line 1798 main::read_in_excel_file_into_array('test2.xls', 'ARRAY(0x2b4a +334)', 'ANNOUNCEMENT') called at test_unzip_in_place.pl line 107 Win32::OLE(0.1709) error 0x80020009: "Exception occurred" in METHOD/PROPERTYGET "c:\ecdev\src\src_dev\model\sq\scripts\test\test_unzip\test2.xls" at o +lelib.pl line 1297. Can't call method "Activate" on an undefined value at olelib.pl line 1 +298.
So basic problem is that I cannot seem to get hold of $wbook object. If I just open the file and do Ctrl-S to save it and then run the script then everything runs as expected.

In reply to Win32::OLE for Excel fails to open newly generated XLS file by woland99

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.