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

I am having some trouble closing a spreadsheet.
The included Perl opens a spreadsheet, reads the first two cells and then tries to close it.
As the copy of the contents of the MSDOS screens shows, the variable to be set when the workbook is
closed is not set but no error is reported. Can someone explain why this is and how to get a ‘1’ for the
result of closing the workbook?
MSDOS screen content
…..\programs>test-excel-close-move.pz
excel_open_array workbook <Win32::OLE=HASH(0x19ddc94)> excel <Win32::OLE=HASH(0x19ddb44)> res <>
open_close open_res <1>
open_close var 00 <Cell A1> 01 <Cell B1>
open_close close_res <> for <Win32::OLE=HASH(0x19ddc94)> error number <0> message <The operation completed successfully.>
use strict "vars"; use Win32; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; sub excel_open_array($$$$$$) { my ($hist_full, $ref_returned_wkbk, $ref_cell_array, $ref_open_res, $w +ksh_no, $excel_data_range) = @_; my ($excel, $worksheet, $excel_res, $ew_message, $workbook_res); eval { $excel = Win32::OLE->GetActiveObject( 'Excel.Application' ) + }; if( $@ ) { print "[excel_open_array] Excel Installation Failure"; $$ref_open_res = 0; return(); } else { $excel = Win32::OLE->new('Excel.Application', 'Quit') or ($exc +el_res = 0, $ew_message = "Could not create excel object"); $$ref_returned_wkbk = $excel->Workbooks->Open($hist_full) or +($workbook_res = 0, $ew_message = "Could not open excel workbook for +<$hist_full>"); print "[excel_open_array] workbook <$$ref_returned_wkbk> excel + <$excel> res <$workbook_res>\n"; $worksheet = $$ref_returned_wkbk->Worksheets($wksh_no); $$ref_cell_array = $worksheet->range($excel_data_range)->{'val +ue'}; $$ref_open_res = 1; } } sub open_close ($) { my ($hist_full) = @_; my ($returned_wkbk, $cell_array, $open_res, $close_res, $wksh_no, $exc +el_data_range); my ($en_num, $en_mes, $var00, $var01); $wksh_no = 1; $excel_data_range = "A1:Z100"; excel_open_array($hist_full, \$returned_wkbk, \$cell_array, \$open_res +, $wksh_no, $excel_data_range); # check on the open result print "[open_close] open_res <$open_res>\n"; # read the first two cells $var00 = $cell_array->[0][0]; $var01 = $cell_array->[0][1]; print "[open_close] var 00 <$var00> 01 <$var01>\n"; # close workbook $close_res = $returned_wkbk->close(); $en_num = Win32::GetLastError(); $en_mes = Win32::FormatMessage($en_num); print "[open_close] close_res <$close_res> for <$returned_wkbk> error +number <$en_num> message <$en_mes>\n"; } #main my $hist_full = "C:\\test-spreadsheet.xls"; open_close ($hist_full);

Replies are listed 'Best First'.
Re: Closing Spreasheet problems
by Bloodnok (Vicar) on May 20, 2009 at 11:32 UTC
    Hmmm ,

    Does ...error number <0> message <The operation completed successfully.> not suggest to you that the sheet was closed successfully ?

    IIRC, methods in Win32::OLE tend to return errors accessed via the Win32::OLE::LastError() method.

    A user level that continues to overstate my experience :-))
      I agree it does suggest that. However I have found that getting
      error messages is this way does not always 'tell truth'
Re: Closing Spreasheet problems
by Anonymous Monk on May 20, 2009 at 09:27 UTC
    Maybe its supposed to be uppercase, Close. Maybe Close doesn't return a value.
      Thanks for your comments.
      I tried close with an upper case C but no difference.
      I have had a 1 in other cases.