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

Hi All,

I'm using win32::ole module to open an excel file but unfortunately the file is not opening

It seems that excel flashes on screen for a milli second and goes off

I understand that there are lot of posts regarding win32::ole module but i'm unable to find a scenario that matches mine and hence posting the question, please guide me.

Microsoft office version i'm using is 2003 and 2010. Have installed Strawberry Perl on Windows systems and OS are Win XP and also Win 7. Tried on both OS's, but result is same

Code used is as follows

#!/usr/bin/perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; my $excelfile = 'd:\\test\\Korea_Checklist.xls'; my $Excel = Win32::OLE->new( 'Excel.Application', 'Quit' ); $Excel->{'Visible'} = 1; my $Book = $Excel->Workbooks->Open("$excelfile") or die "$!";

After execution excel flashes and goes off. Further it says "Died at test.pl line 11."

Replies are listed 'Best First'.
Re: Open an excel file using win32::ole module
by marto (Cardinal) on Aug 12, 2014 at 13:44 UTC

    Make this change and test your example again:

    my $Excel = Win32::OLE->new( 'Excel.Application', 'Quit' );

    becomes:

    my $Excel = Win32::OLE->new('Excel.Application');

    Or if you want to check for and use any existing Excel instances:

    my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application');
Re: Open an excel file using win32::ole module
by davies (Monsignor) on Aug 12, 2014 at 13:39 UTC

    I'm guessing a bit here, so don't be surprised if I get this wrong. I think it's to do with the doubled backslashes in your file name. Because you are using single quotes, escaping doesn't happen and the literal string is used as the path. Since Losedows can't support unnamed directories (between the pairs of backslashes), it can't find your file. Because you have specified the 'Quit' option when you create the Excel instance, this closes Excel when the code exits. This happens when you open or die. Try (a) single backslashes or double quotes, (b) removing the "quit" option, (c) not dying. If none of these work (or give you the clues to getting it to work), please tell us what happened.

    Regards,

    John Davies

      Because you are using single quotes, escaping doesn't happen and the literal string is used as the path.

      perldata:

      String literals are usually delimited by either single or double quotes. They work much like quotes in the standard Unix shells: double-quoted string literals are subject to backslash and variable substitution; single-quoted strings are not (except for \' and \\).

      Or just test it:

      $ perl -wMstrict my $excelfile = 'd:\\test\\Korea_Checklist.xls'; print "$excelfile\n"; __END__ d:\test\Korea_Checklist.xls