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);

In reply to Closing Spreasheet problems by merrymonk

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.