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

I'm using Win32::OLE to open and read spreadsheets. This works fine for a spreadsheet created with Excel, but when I try to use it to open a spreadsheet that I generated using Spreadsheed::WriteExcel, I get this error message:

OLE exception from "Microsoft Office Excel": './test.xls' could not be found. Check the spelling of the file name, +and verify that the file location is correct. If you are trying to open the file from your list of most recently use +d files, make sure that the file has not been renamed, moved, or deleted +. Win32::OLE(0.1709) error 0x800a03ec in METHOD/PROPERTYGET "Open" at C:/workspace/graph.pl line 12

but, the file is there and does exist, in that directory. The code for opening the Excel sheet is:

use strict; use warnings; use 5.010; #use Spreadsheet::WriteExcel; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn=3; my $Excel=Win32::OLE->GetActiveObject('Excel.Application')|| Win32::OLE->new('Excel.Application', 'Quit'); my $book=$Excel->Workbooks->Open("./test.xls"); my $sheet=$book->Worksheets(1);
The code I'm using to create the spreadsheets is (I'm just posting the relevant parts):
use strict; use warnings; use 5.010; use Spreadsheet::WriteExcel; $filename="".$currSite.".xls"; $workbook = Spreadsheet::WriteExcel->new($filename); $worksheet = $workbook->add_worksheet(); while(<$file>){ $worksheet->write($row,0,$year); $worksheet->write($row,1,$count); $row++; } $workbook->close();
I don't know what is wrong. Perhaps there are some permissions that are wrong? Thanks for any help and/or nudges in the right direction.

Replies are listed 'Best First'.
Re: Problem opening WriteExcel generated spreadsheets with OLE
by jrsimmon (Hermit) on Mar 03, 2010 at 21:04 UTC
    The problem has nothing to do with how the spreadsheet is generated. When using your code
    use strict; use warnings; use 5.010; #use Spreadsheet::WriteExcel; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn=3; my $Excel=Win32::OLE->GetActiveObject('Excel.Application')|| Win32::OLE->new('Excel.Application', 'Quit'); my $book=$Excel->Workbooks->Open("./test.xls"); my $sheet=$book->Worksheets(1);
    I get the same error on a spreadsheet I created myself with Excel 2003. Using the slightly modified code
    use strict; use warnings; use 5.010; #use Spreadsheet::WriteExcel; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn=3; my $Excel=Win32::OLE->GetActiveObject('Excel.Application')|| Win32::OLE->new('Excel.Application', 'Quit'); my $book=$Excel->Workbooks->Open("c:\\data\\temp\\test.xls"); my $sheet=$book->Worksheets(1);
    it opens the workbook just fine.
      Well, crap, that works. The weird thing is, with the same exact code in the same folder, it would open one Excel file with the './' path, but not the others. Oh well. Thanks for clearing that up for me!

        Context, young one! Context!

        Internally, on Win32 (<==a context), Perl sees './test.xls', "./test.xls", '.\test.xls', and ".\\test.xls" as the same thing. But if you pass a pathname to something else (oh, say, part of the Windows API known as OLE, another context...), you need to make sure that thing will understand what you mean.

        To the Windows API, OLE, MS-Office, etc and so forth, "./test.xls" has no meaning.

        Capiche?