Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Give focus to existing Application Object

Status
Not open for further replies.

axll

Programmer
Jul 1, 2004
4
US
Hi,

I'm new to VbScript, and am playing around with a little script that will check to see if Excel is open, if so, go to that instance, otherwise startup a new one.

The code I have works, but if I detect an existing Excel application, I can't get it to "task-switch" to it; it just makes the taskbar icon for Excel, flash.

I was able to make it work by creating a WScript.Shell object, and doing an "AppActivate", but I thought I should be able to do it using the existing excel object ("objExcel").

Here is my code ... any help is appreciated.

Dim NewObj
NewObj = false
on error resume next
set objExcel = GetObject(, "Excel.Application")
If objExcel is nothing Then
set objExcel = CreateObject("Excel.Application")
NewObj = true
End If
objExcel.Visible = True
if NewObj = true then
objExcel.Workbooks.Add
end if

 
Try adding this at the end:

objExcel.WindowState = -4143 'xlNormal
 
Thanks for your response. I tried it, but had no luck. It does resize the window, but didn't bring the task up to the front.
 
Hmm, it worked for me in Office 2k on Win2k. Following line also made Excel come to the front of my IE window (and all other windows above it) if it not minimized:
Code:
objexcel.activeworkbook.activate

To test I just opened Excel which starts with "Book1", I brought IE and a few other windows in front of it and rand the script and it put Excel in front of the other windows. Not sure that this will work for you though because the previous line brought Excel to the front regardless of it's window state.
 
Hi, thanks again for your response. I tried this as well, but still no luck.

FYI : I am running Windows XP, and am executing the script from a shortcut on the desktop.

Maybe something in my configuration is causing this strange behavior.

 
Sorry man, I went to an XP system and had same problem and couldn't figure it out.
 
Does WshShell.AppActivate "Microsoft Excel" works ?


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Danp129 - Thanks for all your help, and for confirming that it may be an XP issue, I will look into this.

PHV - Thanks for your advice, it does work, I was just trying to see if there was another way ... I will use the AppActivate call.
 
Hello axll,

Just to add a side-note which does not help directly. It is a hidden design principle/spirit for xp that the os does not allow application's focus be stolen without specificically negotiated through api. A consequence is also what you've observed.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top