http://qs321.pair.com?node_id=979168


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

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.

Replies are listed 'Best First'.
Re^6: Perl r/w Excel with OLE
by Sinistral (Monsignor) on Jul 02, 2012 at 15:57 UTC

    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.

      Hmmm....ok. Perhaps I misunderstood you when you said:

      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.

      That sounded like you plan to use macros for your "automation". Whether you are manually opening Excel and running the macros or using Perl to call the macros, I personally believe that the issues that I pointed out are still relevant.

      Also, by using VBA instead of Perl does not mean you escape from OLE. Any VBA code (external to Excel or inside Excel as macros) that does any Excel automation will be using OLE. The Win32::OLE module lets you access the OLE API from Perl. In other words, if you're automating Excel with VBA, macros, or Perl, you're going to use OLE in some way as far as I know.

      If I'm still misunderstanding your point, my apologies.

Re^6: Perl r/w Excel with OLE
by Anonymous Monk on Jun 29, 2012 at 19:31 UTC