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

Hello.... I've a question about how to use win32::ole function to SaveAs a text file(ascii format) from excel file? Below are my codes, can anyone help to check if there is anything I missed?
## use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Excel'; my $Excel = Win32::OLE->new("Excel.Application"); ........ ....... ....... $Excel->{Visible} = 1; my $Book = $Excel->Workbooks->Open($bom_file_directory); # select worksheet number 1 (you can also select a worksheet by name) my $Sheet = $Book->Worksheets(1); my $new_bom_txt = "c:/valor/tmp/value.txt"; my $out_format = "\"xlText\""; if ( -e $new_bom_txt) { unlink $new_bom_txt; } # $Sheet->SaveAs( $new_bom_txt )->FileFormat = "xlText"; $Sheet->SaveAs( { 'FileName' => $new_bom_txt , # 'FileFormat' => $out_format} ); $Book->Close(); $Excel->Quit; ...........
Please kindly help to solve this question... Thanks a lot.

Edited by Chady -- code tags.

Replies are listed 'Best First'.
Re: How to save a text file from excel
by tachyon (Chancellor) on Jul 08, 2004 at 02:19 UTC

    xlText is a CONSTANT (which has the value -4158 ) so you don't quote it as this makes it a string. You also need to disable DisplayAlerts or you will get prompted with the usual are you sure BS.

    use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Excel'; my $Excel = Win32::OLE->new("Excel.Application" , sub { $_[0]->Quit } +) or die Win32::OLE::LastError; $Excel->{Visible} = 1; my $Book = $Excel->Workbooks->Open("c:\\test.xls") or die Win32::OLE::LastError; my $Sheet = $Book->Worksheets(1); $Excel->{DisplayAlerts} = 0; # avoid being prompted $Sheet->SaveAs( { FileName => "c:\\test.txt" , FileFormat => xlText } +) or die Win32::OLE::LastError; $Book->Close(); $Excel->Quit;

    cheers

    tachyon

Re: How to save a text file from excel
by knoebi (Friar) on Jul 08, 2004 at 07:36 UTC