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

Note: In the following code snippet, &w_die is a routine to write the error message to a log file before dying.

This code runs fine if I am actually logged in. However, when I try to set it up in Windows (Control Panel -> Scheduled Tasks) to read from an Excel file each day at a given time when I am not logged in, it fails.

(I AM able to read from an Oracle database this way using Scheduled Tasks when I am not logged in, with

use Win32::ODBC;
However, trying to do the same thing with Excel when not logged in fails.)

Here is the snippet:

... use OLE; # will read Excel workbooks ... eval {$excel = Win32::OLE->GetActiveObject('Excel.Application')}; &w_die("Excel not installed") if $@; ... $excel = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or &w_die("Sorry, cannot start Excel\n"); ...
End of snippet.

The result in my logfile is:

Sorry, cannot start Excel

Again, this works fine if I am actually logged in. How can I get Perl to read from an Excel file at a given time when I am not logged in?

Thank you

Replies are listed 'Best First'.
Re: When not logged in, using Win32::OLE for Excel fails
by Corion (Patriarch) on Jan 04, 2010 at 16:15 UTC

    You cannot launched Excel without a connected desktop and other things.

    You have two possible approaches. Either have your service start up its own login session, mount the appropriate registry hive and set up everything as it should be. Or, use Spreadsheet::ParseExcel, which does not need Excel to read a spreadsheet.

    Update: Also, maybe use Spreadsheet::Read, which can read more than just Excel files.

      There may be another solution. I was experiencing a similar problem with Win32::OLE for excel after I migrated some scripts from a windows server 2003 to a windows 2008 server. I use Spreadsheet::WriteExcel to create a spreadsheet but as I want it in excel 2007 format (.xlsx) I use Win32::OLE to convert it from 2003 to 2007 format.

      my $exc = Win32::OLE->new('Excel.Application','Quit') || die "could no +t start excel\n"; $exc->{Visible} = 0; #runs excel in the background $exc->{DisplayAlerts} = 1; my $book = $exc->Workbooks->Open("$path") || die ("Can't open file $pa +th ", Win32::OLE->LastError()); $book->SaveAs({FileName => "$convto", FileFormat => 51}) || die ("Can' +t save as file format ", Win32::OLE->LastError()); $book->Close(); $exc->Quit();

      I did some searching and found that all I needed to do to fix is was to create the directory c:\windows\syswow64\config\systemprofile\desktop (or system32 instead of syswow64 on a 32 bit system). After I did that it all worked fine. Here's the posting I got that trick from:

      http://social.msdn.microsoft.com/Forums/en-ZA/olbasics/thread/e437524b-412a-4ed3-9cf8-fb9afed7a7ad

      Hope this helps!