Solved

Have Excel determine window focus?

Posted on 2014-11-07
10
119 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?
0
Comment
Question by:RWayneH
  • 5
  • 4
10 Comments
 
LVL 1

Expert Comment

by:Angelo Mileto
Comment Utility
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.
0
 
LVL 2

Expert Comment

by:Glen Richmond
Comment Utility
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?
0
 

Author Comment

by:RWayneH
Comment Utility
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?
0
 
LVL 2

Expert Comment

by:Glen Richmond
Comment Utility
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
0
 
LVL 2

Expert Comment

by:Glen Richmond
Comment Utility
obvilousy you will need to change the Dim object type too, probably to just Object unless you register QTP in the COM references.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:RWayneH
Comment Utility
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.
0
 
LVL 2

Accepted Solution

by:
Glen Richmond earned 500 total points
Comment Utility
do you have any version of Visual Studio Express installed or greater?
Version 12 for desktop i have put togeter small app to list the tasks and get the details from the Op system.
unfortunatly it uses a reference to 4.5 .Net framework that cant be included as a ref in Excel.
But it will allow you to interigate the QTP process to get he generic name you need to use In GetObject
rather than having to use full path.
This class name once discovered using this tool will be the same on all machines and ergo can be used in  your code safley without worry over pathing issues.


Source code attached for lising app.
also example code :-
Public Class Form1

    Private m_Results As List(Of ManagementObject)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
        e As System.EventArgs) Handles MyBase.Load
        m_Results = New List(Of ManagementObject)

        Dim searcher As New ManagementObjectSearcher("SELECT * " & _
            "FROM Win32_process")
        For Each result As ManagementObject In searcher.Get()
            lstCaptions.Items.Add(result.Item("Caption"))

            m_Results.Add(result)
        Next result
    End Sub


    ' Show the detail for the selection.
    Private Sub ShowDetail()
        If lstCaptions.SelectedIndex < 0 Then Exit Sub

        Dim result As ManagementObject = _
            m_Results.Item(lstCaptions.SelectedIndex)

        Dim txt As String
        'If radMof.Checked Then
        txt = result.GetText(TextFormat.Mof).ToString()
        'ElseIf radCimDtd20.Checked Then
        'txt = result.GetText(TextFormat.CimDtd20).ToString()
        'Else
        'txt = result.GetText(TextFormat.WmiDtd20).ToString()
        'End If

        txt = txt.Replace(vbLf, vbCrLf)
        txt = txt.Trim()
        txtDetails.Text = txt
    End Sub


    Private Sub lstCaptions_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCaptions.SelectedIndexChanged
        ShowDetail()
    End Sub


End Class

Open in new window


Also if you dont have VS Studio Express, i suggest you get it, its free and is a great tool!
http://www.microsoft.com/en-gb/download/details.aspx?id=34673
WindowsApplication3.zip
0
 
LVL 2

Expert Comment

by:Glen Richmond
Comment Utility
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.
0
 

Author Comment

by:RWayneH
Comment Utility
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?
0
 

Author Closing Comment

by:RWayneH
Comment Utility
Thanks for the help.
0

Featured Post

Shouldn't all users have the same email signature?

You wouldn't let your users design their own business cards, would you? So, why do you let them design their own email signatures? Think of the damage they could be doing to your brand reputation! Choose the easy way to manage set up and add email signatures for all users.

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now