Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Win32 Active Window

by esr (Scribe)
on Dec 06, 2004 at 23:58 UTC ( #412785=perlquestion: print w/replies, xml ) Need Help??
esr has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl script which is creating/editing both a Word document and an Excel spreadsheet. When I am done with this I want to be sure that the Excel spreadsheet is in front -- i.e. the "active window". I had hoped that the order in which they are made visible and/or maximized would do it but which one appears in front appears to be random. I have been unable to find anything that looks like the function I want in Win32::OLE but I am assuming there is an object there someplace. I can inquire about ActiveWindow but I can't find info on how to set the ActiveWindow. Can someone point me to the thingy I need?

Replies are listed 'Best First'.
Re: Win32 Active Window
by Albannach (Prior) on Dec 07, 2004 at 04:54 UTC
    I do this with Win32::GuiTest's SetForegroundWindow function. I believe the docs have an example using Excel which will get you going.

    I'd like to be able to assign to an luser

Re: Win32 Active Window
by fglock (Vicar) on Dec 07, 2004 at 00:28 UTC

    I'm not familiar with Win32::OLE, but the function to look for is called "setfocus". Hope this helps.

Re: Win32 Active Window
by EdwardG (Vicar) on Dec 07, 2004 at 12:53 UTC

    This works for me -

    use Win32::GuiTest qw/ FindWindowLike SetForegroundWindow GetDesktopWindow IsWindowVisible /; my @handles = grep {IsWindowVisible($_) } FindWindowLike(GetDesktopWindow(), qr/^Microsoft Excel/, undef, undef); die "Too many windows\n" if (@handles > 1); die "Not enough windows\n" if (@handles < 1); my $excel = shift @handles; SetForegroundWindow($excel);


      If the only window you are competing with is the Word Window the following is tested and works on 5.8.0 on XP-SP2 with Office 97. You could probably just not make the word window visible also .. however if they both need to be in the taskbar.. this works fine.

      use strict; use warnings; use Win32::OLE; use Win32::OLE::Const; my $xl = Win32::OLE->new('Excel.Application'); my $xlConst = Win32::OLE::Const->Load('Microsoft Excel 8.0 Object Libr +ary'); #Add a workbook so the window doesn't autoclose $xl->Workbooks->Add(); my $word = Win32::OLE->new('Word.Application'); my $wordConst = Win32::OLE::Const->Load('Microsoft Word 8.0'); #Set both visible to test $word->{Visible} = -1; $xl->{Visible} = -1; #Minimize Word $word->{WindowState} = $wordConst->{'wdWindowStateMinimize'}; #Maximize Excel $xl->{WindowState} = $xlConst->{'xlMaximized'};
Re: Win32 Active Window
by Stevie-O (Friar) on Dec 07, 2004 at 02:52 UTC
    This will be harder than you think -- Windows is specifically written to make it difficult for you to accomplish what you're doing, because the *computer user* is in charge of bringing stuff to the front if they want to.
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
Re: Win32 Active Window
by erix (Parson) on Dec 07, 2004 at 10:50 UTC

    I'm not sure it applies to your problem but the attribute /function to look for might be called 'BringToFront'.


Re: Win32 Active Window
by maa (Pilgrim) on Dec 07, 2004 at 12:07 UTC

    I've not tested this, but how about toggling the Excel Applications Visible property?

    #Assuming your OLE Excel.Application object is called $Excel $Excel->{Visible}=0; $Excel->{Visible}=1;

    Or how about toggling the WindowState property of the Excel object?

    If it were the other way around it would be easy since Excel has an ActivateMSApp method. Unfortunately, WINWORD does not...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://412785]
Approved by johnnywang
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2018-05-25 08:00 GMT
Find Nodes?
    Voting Booth?