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

     Dearest monks, I desperately need your wisdom and patience...
     I've been working on NT4 SP6 with Outlook 2000 (9.0.0.2711). My perl program retrieves information from the Outlook object by using the object model provided with the VB editor. So far so good... I need to find whether there's an active Outlook window open. Hence I use the code
$app = Win32::OLE->GetActiveObject('Outlook.Application');.
This code works just fine and provided there actually is a window open, the OLE function returns a reference to that window.
     However, when I try the code with Win98 and Outlook 2000 SR1 (9.0.0.3821), the function always returns nothing (ie undef). This's been driving crazy because I didn't expect the error to be there and I'd been looking elsewhere. Furthermore the function Win32::OLE->new works fine on both versions of Outlook which makes it even more frustrating.
     Has anyone encountered such a problem before ? Please help me out...
Heureux qui, comme Ulysse, a fait un beau voyage
Ou comme celui-là qui conquit la Toison,
Et puis est retourné plein d'usage et raison,
Vivre entre ses parents le reste de son âge!

J. du Bellay, poëte angevin

Replies are listed 'Best First'.
Re: Another Win32::OLE problem
by NetWallah (Canon) on Jun 25, 2003 at 16:26 UTC
    Are you launching outlook from the perl program ?

    If so, you may want to look at Microsoft Knowledge Base Article - 238610, which states

    Although the Office application is running, it might not be registered in the Running Object Table (ROT). A running instance of an Office application must be registered in the ROT before it can be attached to using GetObject (Visual Basic) or GetActiveObject (Visual C++). When an Office application starts, it does not immediately register its running objects. This optimizes the application's startup process. Instead of registering at startup, an Office application registers its running objects in the ROT once it loses focus. Therefore, if you attempt to use GetObject or GetActiveObject to attach to a running instance of an Office application before the application has lost focus, you might receive one of the errors above.
    Another suggestion is to look at, and possibly update the win98 version of OLEAUT32.DLL, which implements the getactiveobject function.
      Actually I'm starting to think that Win98 may be the problem. I'll update the dll as you mentioned - it's a great idea - thanks so much.
      Considering the OLE function works fine with Word, Powerpoint, Excel, considering the OLE->new function works with all the software even Outlook, considering only one specific version of Outlook doesn't work (or is it because it's running under Win98), I tend to believe Win98 and its dll may be the problem or else, perhaps the OLE package.
      I'll run the same bit of code with Outlook 2002 under Win2k and I'll let you know whether it works.
Re: Another Win32::OLE problem
by Zero_Flop (Pilgrim) on Jun 26, 2003 at 05:33 UTC
    Foggy, not sure what your problem is. I would lose focus on outlook as mentioned above. I would also check that version of Outlooks VBA help for a hint in any changes in implementation. You should also get familiar with the perl debugger or since you are on windows checkout "Open Perl IDE" While I prefer vim, opIDE has a nice gui for debugging.

    Also, you should have a die or warn at the end of: $app = Win32::OLE->GetActiveObject('Outlook.Application');.
      Thanks guys for your help, Actually, it seems a bit more complicated than just focus/blur. As a matter of fact, my perl program works fine with just about everything except Outlook SR1 9.0.0.3821. The program does even detect that Outlook is open by using GUI properties (Outlook's class name : rctrl_renwnd32). For instance, the following bit of code works fine :

      return Win32::GUI::GetClassName(Win32::GUI::GetForegroundWindow());

      So far the only property that doesn't seem to work is

      $app = Win32::OLE->GetActiveObject('Outlook.Application');

      Besides, Zero Flop, thanks for reminding me about the die or warn - but I don't need it since I analyse the return value for $app - if it's undef then it's not open.
Re: Another Win32::OLE problem
by Foggy Bottoms (Monk) on Jun 27, 2003 at 10:19 UTC
    Some news... I tried my bit of code with Win2k and the latest version of Outlook : it works fine. Hence I tend to think Win98 was indeed what was causing the code not to work. Thanks everyone for all your help.
Re: Another Win32::OLE problem
by Grygonos (Chaplain) on Jun 25, 2003 at 16:11 UTC
    First I would update your nt4 machine to outlook 2000 sp1 if you can. That will help in determining if the problem lies in the Outlook version change, or the OS version change. Maybe the object in sp1 doesn't use the method the same way, or something along those lines. Hope this helps you on your debugging quest.
      The problem is... it's the older version that's working fine... And I don't really understand why this particular OLE->GetActiveObject method won't work with the latest Outlook version if the OLE->new function does work... Thanks for your idea though. Unfortunately, I can't upgrade...