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

I am having trouble saving an Excel spreadsheet. With Perl I take the steps
1. Take a copy of a spreadhsheet that has some cells already filled in
2. Use this copied file with
$excel = Win32::OLE->new('Excel.Application', 'Quit'); $workbook = $excel->Workbooks->Open<copied file name>) $worksheet = $excel->ActiveWorkbook->Worksheets(“sheet1”);
3. Fill in the data I need
4. Try and save the edited copy of the spreadsheet with
$excel->Workbooks(1)->SaveAs(<copied file name>) or ($excel_res = 0, + $ew_message = "Could not save spreadsheet <copied file name>); $en_num = Win32::GetLastError(); $en_err = "message <" . Win32::FormatMessage($en_num) . "> number <" . + $en_num . ">"; $ew_message = "<" . $ew_message . "> is just after saving part file\n" + . "system error text <$en_err>\n";
The MSDOS screen gives the following contents for $ew_message
<Could not save spreadsheet <copied file name> is just after saving part file system error text <message <> number <317>>
I also get an Excel message saying Do want to save the changes you made to <copied file name> with three buttons for Save, Don’t Save and Cancel.
If I open the copied spreadsheet (before I use any of the message buttons) I get the message that the file is Locked for Editing.
If I use the Save button I get the edited spreadsheet that I want.
What changes do I have to make so that the spreadsheet will be saved as I would like it to?

Replies are listed 'Best First'.
Re: Excel SaveAS problem
by poj (Abbot) on Mar 21, 2017 at 15:39 UTC

    If you are not changing the name then use Save not SaveAs(newname)

    #!perl use strict; use warnings; use Win32::OLE; $Win32::OLE::Warn = 3; my $filename = 'c:/temp/Book2.xlsx'; my $excel = Win32::OLE->new('Excel.Application', 'Quit'); my $book = $excel->Workbooks->Open($filename) or die Win32::OLE->LastError(); my $sheet = $book->Worksheets('sheet1'); $sheet->Range("A1")->{Value} = localtime; # update something $book->Save;
    poj
      Many thanks - that cured the problem!