http://www.perlmonks.org?node_id=978993


in reply to Re^2: Perl r/w Excel with OLE
in thread Perl r/w Excel with OLE

I'm afraid I have to disagree extremely strongly with some of the advice you've given here. Some of the issues are minor, such as always hiding the spreadsheet in production code. What if I want to present the user with the spreadsheet? Usually I hide, but it's definitely horses for courses.

More serious is taking control of an existing instance. For a one-off script, this can be a useful club to have in your bag, but I've written elsewhere (Re: Manipulating open excel sheet) about why this is a bad idea.

However, the thing that really terrifies me is that your code not only takes control of one of my instances of Excel (I frequently run several - it can be essential to do this), but then closes it. My work! Where has all my work gone? And there's not even an autosave, as those files are automatically deleted when the creating instance exits normally. Now I'll grant you that it can be useful to loop through all instances of Excel and close them if you know for sure that they are all orphans, but the thought of this getting into production code turns me cold even in this weather.

Regards,

John Davies

Replies are listed 'Best First'.
Re^4: Perl r/w Excel with OLE
by Sinistral (Monsignor) on Jun 29, 2012 at 16:26 UTC

    As said in response to dasgar, these are good points. I am not the original author, but it seemed reasonable to include code provided by the authors of Win32::OLE. It's good to point out to future viewers the possible pitfalls of using such code, including the possibility of "hijacking" an existing Excel instance doing other things. As I said, my mental model of Win32::OLE automation has always been a dedicated single instance of Excel running without intervention (or possibility of intervention, in the case of a Server). If I were to do simple macro type automation, then I'd record it within Excel or use the existing VBA and save myself the pain of OLE.

    As for this specific example, if you had an open file already loaded in the Excel instance, what would happen is that you'd get a new sheet added, there'd be data in that, and the result would be saved in test.xls. So if you had a complex workbook already open, those changes could be brought back. It's a fair point, though, you wouldn't want to have any open instances before running such a script.

      Unless there's something going on that I don't understand (in which case I'd love to know), it won't work as you describe. Consider

      # get a new workbook $book = $ex->Workbooks->Add;

      This creates a new book, i.e. a new file, in the Excel instance. Then, when the code saves test.xls, it's only that book that gets saved. No sheets are added. $sheet is created by the line

      $sheet = $book->Worksheets(1);

      Again, this is in $book, not in anything that might have been open in the same instance. This can't fail, as the creation of $book will have the default number of sheets from SheetsInNewWorkbook, which can never be less than 1.

      Now, I accept that the code given,

      # save and exit $book->SaveAs( 'test.xls' ); undef $book; undef $ex;

      doesn't actually close the instance of Excel, but merely undefines it in Perl. But this thread is, to a large extent, about closing instances of Excel, so my fear is undiminished.

      If you don't have any instances of Excel open, there's no point even trying to take control of an open instance. And if you do, we return to the dangers on which we agree. If the code creates its own instance without trying to take control of an existing instance, you are safe either way.

      Regards,

      John Davies

      If I were to do simple macro type automation, then I'd record it within Excel or use the existing VBA and save myself the pain of OLE.

      I don't want to sound overcritical, but wanted to point out two potential problems with having Perl calling macros in Excel.

      First, Excel 2010 defaults to not enable macros. That means you'll probably have to add code to enable the execution of macros. Since I don't have Perl calling macros, I personally have not encountered this issue myself. That default behavior might be changeable, but I personally think that it's not a good idea to make assumptions about what settings are for a program like Excel when developing code and manipulates that program.

      The second problem is related to something actually happened at work. My first dive into learning Perl was to generate Excel charts. A coworker previously had a script that did this and his called macros. My coworkers were emphatically telling me to not use macros when they found out what I was working on. What happened was my coworker's template Excel file got corrupted with a virus. So when folks used his script, the macros was deleting files, which wreaked havoc on projects. Therefore, I made sure that my code was not using macros and was not relying on a template Excel file.

      Again, it's a matter of personal preference. However, I would strongly urge folks to not call macros from Perl, but rather convert the macros into Perl code for your Perl program to use. I realize that doing so means more work, but I personally feel the benefits of doing so are well worth it.

        No, you misunderstood my point. I didn't mean "use macros that are invoked by Perl using Win32::OLE". I meant "Use the macro recorder and VBA instead of using Perl". VBA and the macro recorder (which ultimately is a shiny wrapper on the underlying VBA) are the tools explicitly designed by Microsoft for automating Excel. It's just that with the power of Perl, you can do many other things that are difficult or impossible when using VBA.

        After Davies explained, I now see how reusing a single instance of Excel could lead to data loss. Perhaps the authors of Win32::OLE should be contacted so that the module examples have caveats about what it really means for the system when you reuse a running Excel instance.