We help IT Professionals succeed at work.

Have Excel determine window focus?

RWayneH
RWayneH asked
on
264 Views
Last Modified: 2014-12-05
Is there a way in VBA to ask Excel, what are the active windows running and set focus to the one I want?
I am running a process in QTP that dips into a Excel file that will login me into SAP.  However SAP keeps the focus and I cannot get it back to QTP after SAP is running.  Thinking the Application.SendKeys {"%Tab"} or something simuliar, but prefer looping through the active window sessions a picking the proper one to get around having to use SendKeys.

Has anyone come across this? and how did you get back to the session that you need?
Comment
Watch Question

If you have multiple files,
    Windows(QTPFile.xls).Activate - or whatever it's called

If it's all in one file and/or you want a specific sheet
    Sheets("Sheet Name").Select

I've used both of these in the past with success.
Glen RichmondSenior Software Developer.

Commented:
is the code behind the calling program that logs into SAP still runing at this time or has it opened SAP in a modal fasion and waiting for it to be dismissed before continuing?

Author

Commented:
Yes it is still running, The code behind opening SAP is just to do that.  Unless you know of a way to do this directly in QTC, I will not have to bounce out of QTP run it and go back in.  I need to shift focus back to QTP so it can perform the rest of the test, even though technically I could complete the test in Excel (which I used to log into SAP)  I did get it to work by using the following:

AppActivate ("QuickTest Professional - [C:\QTP\CreateSO_BlankHeader*]")

However it uses the application name and the full file path.  Is there a way to make this more dynamic?  When I reuse this I do not want to have to dal with exact folder paths.  Is there a way to use a MID on this and just search by maybe "QuickTest Pro"??  or collect all session Titles and pull in from that group?
Glen RichmondSenior Software Developer.

Commented:
could you not connect to QTP via com and have control over it that way?

I dont knwo how you open QTP to Log on, but you can always GetObject from the Process queue.
ive used this to connect to Excel in the past a kill any of the open apps..

Look in the task manager and try it?

Sub KillAnyExcel()
		
		On Error GoTo killerr
		
		Dim XlsToKill As Excel.Application
		XlsToKill = GetObject( , "Excel.Application")
		
		XlsToKill.DisplayAlerts = False
		XlsToKill.Quit()
		
		XlsToKill = Nothing
		
killerr: 
		Exit Sub
		
	End Sub

Open in new window


http://msdn.microsoft.com/en-us/library/e9waz863(v=vs.90).aspx
Glen RichmondSenior Software Developer.

Commented:
obvilousy you will need to change the Dim object type too, probably to just Object unless you register QTP in the COM references.

Author

Commented:
is there a way to loop thru session and gather names?  Then if one of them has MID "QuickTest Pro"  ?? make that one active session.
Senior Software Developer.
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Glen RichmondSenior Software Developer.

Commented:
ill also see if there is a way to ref the system dll in excel to enumarate the Tasks list, so you can cycle tasks in excel as you requested.

Author

Commented:
I do not have Visual Studio Express, nor will the users that will need to run this.  Is there a way to do this directly in VBA?

Author

Commented:
Thanks for the help.