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

Dear monks, may I ask for your wisdom once again ? My problem is rather simple and is more of a conceptual thing than a coding problem.
I'm currently working on a project which requires me to communicate with applications such as word or excel. The line of code to retrieve the currently open object (document or workbook) is the following :
my $excel = Win32::OLE->GetActiveObject('Excel.Application')
(for Excel). However I don't really like the idea of actually typing in the name of the class 'Excel.Application'. I'd rather read it in the registry.
Do you think it's necessary ? Or are we sure the class name's never going to change ?

Replies are listed 'Best First'.
Re: Class names in OLE
by krisahoch (Deacon) on Jun 17, 2003 at 11:08 UTC
    Foggy Bottoms,

    First off, that is a unique and though provoking name.

    IMPO, it may be more work than it is worth to go looking in the registry. I agree that typeing 'Excel.Application' is a pain in the back side so here are a couple of UNTESTED alternatives...

    First Method

    # use Constants use constant EXCEL => 'Excel.Application'; use constant WORD => 'Word.Application'; my $excel = Win32::OLE->GetActiveObject(&WORD)

    Second Method

    #Use another package package MSApps; { our $MSApps::EXCEL = 'Excel.Application'; our $MSApps::WORD = 'Word.Application'; } ## Then use MSApps; my $excel = Win32::OLE->GetActiveObject($MSApps::EXCEL)

    Third Method

    #Do the most simple thing: create variables. my $EXCEL = 'Excel.Application'; my $WORD = 'Word.Application'; my $excel = Win32::OLE->GetActiveObject($WORD)

    UpdatE: Eccentric reformating


    Kristofer Hoch

    Si vos can lego is, vos es super erudio

      Si vos can lego is, vos es super erudio Interesting little phrase actually... But I believe it's not correct latin, or is it ?
      If you can read this, you are really smart. Am I right ? Then it should be si istud legitis, doctissimi estis. (I studied latin a while back and still get a kick out of it - it's really cool).
      ... Anyways... as for my nickname, it comes from a metro station in DC where I used to live.

      And now back to perl, I did use constants but I was worrying about the fact that the names would change quickly. But apparently, it'd be too much trouble to go and read the registry.
      Thanks for your help, nice meeting you.
      Si vos can lego is, vos es super erudio
      If you can read this, you are over educated;)

      Kristofer Hoch

      Si vos can lego is, vos es super erudio

Re: Class names in OLE
by gellyfish (Monsignor) on Jun 17, 2003 at 10:54 UTC

    For myself I would be fairly confident that this is not going to change any time real soon now and I think that even if it were to do so it is likely to retain the original ProdID as an alias.

    However if you were going to look it up yourself you should bear in mind that the way that it is designed is that the registry entries map the ProgId to the ClsId guid rather than vice versa and also that the ProgId is not guaranteed to be unique.

    /J\
    
Re: Class names in OLE
by aquarium (Curate) on Jun 17, 2003 at 11:53 UTC
    go ahead and hardcode, MSOffice apps do. By the time the names change, the interface is likely to change also, and you'll need to re-write anyway.
      Great, thanks everyone !
Re: Class names in OLE
by Anonymous Monk on Jun 17, 2003 at 11:41 UTC
    Hi, You should goto CPAN.org and check out the module: Spreadsheet::WriteExcel that can help you work with excel
Re: Class names in OLE
by Nkuvu (Priest) on Jun 17, 2003 at 17:58 UTC

    Aside from the original question (which I think has been answered quite well) I have a question for you. What happens if your Excel application isn't running? Then the Perl script wouldn't be able to GetActiveObject at all. I'd suggest the following addition:

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

    And of course, check the return code to make sure things went smoothly. But that goes without saying, right? :)

      Hi,
      I too have been dabling the dark OLE arts recently. You can check to see if the excel object is open, and then if not create a new worksheet.
      On the same note, you could just open the specific file instead of the active object as well.
      $Obj = Win32::OLE->GetObject('c:\somefile.xls');

      However! The article to read would be: here
      Dave -- Saving the world one node at a time
      Well of course you guys are right - you do have to check whether the excel application is running... or else...
      But actually I check in another package so I know it's there and open. My purpose is to read the currently active document whether saved or not so that's why I use the activeWorkbook property - working with OLE is actually loads of fun !!!
      . Thanks guys and have a nice day.
      ERROR!

      What you mean is
      my $excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32: +:OLE->new('Excel.Application');
      That is, s/or/||/.

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.