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

Esteemed Monks:
I am using Perl to add some charts to an Excel spreadsheet generated in 2003 XML on another system. I can open the XML file and add the charts but when I try to save the modified spreadsheet in the 2007 xlOpenXMLWorkbook format, I get this error:
OLE exception from "Microsoft Office Excel"
Unable to get the SaveAs property from the Workbook class
Win32::OLE(0.1709) error 0x800a03ec
in METHOD/PROPERTY "SaveAs" in PerlMonks.pl line 31

Here is the code the error message refers to:

#!/usr/bin/perl -w use diagnostics; use diagnostics -verbose; use strict; use Cwd; use Win32::OLE; use Win32::OLE::Const "Microsoft Excel"; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); $Excel->{Visible} = 1; $Excel->{DisplayAlerts} = 0; my $dir = cwd(); $dir =~ s/\//\\/g; my $path = $dir . "\\PerlMonksTest.xml"; my $Book = $Excel->Workbooks->OpenXML($path); $Book->Activate; my $new_file= $dir . "\\PerlMonksTestWB.xlsx"; if(-e $new_file) {unlink $new_file or die "Could not remove '$new_file +': $!\n";} $Book->SaveAs( {Filename => "$new_file", FileFormat => 'xlOpenXMLWorkb +ook', CreateBackup => 'False'} ); #$Book->SaveAs( {Filename => "$new_file"} );

When I use the SaveAs in line 32 instead of 31, I don't get the error, but the workbook is saved as 2003 XML, which doesn't support charts (as far as I can tell).

I can supply the XML file referred to in the code above (PerlMonksTest.xml) if necessary.

Thanks for your help.

Replies are listed 'Best First'.
Re: Problem using SaveAs when trying to change Excel format
by Anonymous Monk on Nov 06, 2009 at 18:16 UTC
    xlOpenXMLWorkb is supposed to be a number (read microsoft constant), not the string 'xlOpenXMLWorkb'

      Thanks. That fixed it

        Hi, I' new to perl. I'm appending a row in the excel file. But a pop comes out(to save the temp file) while saving a file. Below is the code I'm using to do the same.

        my $Excel = Win32::OLE->GetActiveObject('Excel.Application') | +| Win32::OLE->new('Excel.Application'); $Excel->{'Visible'} = 0; #0 is hidden, 1 is visible $Excel->{DisplayAlerts}=0; #0 is hide alerts # Open File and Worksheet util->logit("[$PARENT_PROC_NAME]: in_file[$in_file]"); my $Book = $Excel->Workbooks->Open($in_file); # open Excel file my $sheetcount = $Book->Worksheets->Count; #util->logit("[$PARENT_PROC_NAME]: sheetcount[$sheetcount]"); foreach my $i (1 .. $sheetcount) { #util->logit("[$PARENT_PROC_NAME]: i value [$i]"); my $Sheet = $Book->Worksheets($i); if(defined($Sheet)) { util->logit("[$PARENT_PROC_NAME]: Sheet[$Sheet]"); my $LastRow = $Sheet->UsedRange->Row + $Sheet->UsedRange-> +Rows->Count - 1; util->logit("[$PARENT_PROC_NAME]: LastRow[$LastRow]"); $Sheet->Cells($LastRow+1,1)->{Value} = localtime; } } #$Book->SaveAs(Filename =>$in_file); #my $file_ointer = $Book->Save(); #$Book->SaveAs(); $Book->SaveAs(); $Book->Close(); $Book->Quit(); undef $Book;

        I need to save the changes without any manual efforts... Kindly suggest something. Thanks in advance.