Activating (bring to foreground) a specific window with vbscript

Tom K. picture Tom K. · Feb 4, 2016 · Viewed 21.5k times · Source

I'm not even sure where to start with my question, I tried a hundred things and googled for hours but didn't find anything useful. (I'm open to every dirty trick.)

Here's my problem:

I have a .hta-file with a listbox that looks like this:

hta file

It lists all sessions/modi of my SAP Gui running.

        Set SapGuiAuto  = GetObject("SAPGUI")
        Set application = SapGuiAuto.GetScriptingEngine

        If application.Connections.Count > 0 Then
            Set connection  = application.Children(0)

            If connection.Sessions.Count > 0 Then
                Set session = connection.Children(0)
            End If
        End If


        If IsObject(WScript) Then
            WScript.ConnectObject session,     "on"
            WScript.ConnectObject application, "on"
        End If

Set optGroup = Document.createElement("OPTGROUP")
    optGroup.label = "Server"


    'count all connected servers 
    ConnectionCount = application.Connections.Count




        If ConnectionCount > 0 Then
            Sessionlist.appendChild(optGroup)

            Else 
            optGroup.label = "No connection here."


        End If
        'count all sessions per server


        If ConnectionCount > 0 Then
            For Each conn in application.Connections

                'Text output connections and sessions

                SessionCount = conn.Sessions.Count
                whatIsIt  = conn.Description
                ConnectionFeld.innerhtml = ConnectionFeld.innerhtml & " <br> " & SessionCount & " Sessions auf " & whatIsIt

                'fill listbox with all connections

                Set objOption = nothing
                Set optGroup = Document.createElement("OPTGROUP")
                optGroup.label = conn.Description
                Sessionlist.appendChild(optGroup)

                i = 0

                    'fill listbox with all sessions
                    For Each sess In conn.Sessions

                        i = i + 1
                        Set objOption = Document.createElement("OPTION")

                            objOption.Text = "Session " & i & ": " & sess.ID
                            objOption.Value = sess.ID
                            SessionList.options.add(objOption)

                    Next
            Next

        Else 

        Exit Sub

        End If

My goal: When I doubleclick on one of the entries in that list, the selected instance of my SAP Gui should come to the foreground/get activated.

Unfortunately my taskmanager only lists one task and that is "SAP Logon". One of my opened windows also has the name "SAP Logon", all others have the same name: "SAP Easy Access".

enter image description here

The only way I can see the IDs of the connection (servername) and the IDs of the session is via extracting them with vbscript. (see above)

Is there any way to do that? The only workarounds I could think of after trying a thousand solutions are these two:

extremely ugly workaround:

If sessionID = sess.ID Then

Set objShell = CreateObject("shell.application")
objShell.MinimizeAll

sess.findById("wnd[0]").maximize

End If

It minimizes all windows an then maximizes the selected SAP window. Unfortunately My HTA-GUI also gets minimized which kinda sucks.

Second idea:

Somehow get to these clickable thingies by shortcut and put that in my script or some other ugly way.

By hand you have to do this:

Click on that little arrow, rightclick on the icon and then leftclick on the name.

info tray symbols

Is there any way to automate this? It's driving me crazy.

Hope someone can help me, it would be GREATLY appreciated.

PS: I'm sitting on a machine with restricted rights and so I may not be able to tackle this with Windows API-ish solutions.

EDIT concerning comments:

It is not possible:

  • to change registry entries
  • create COM objects
  • work with anything else than VBScript

Answer

user5721973 picture user5721973 · Feb 4, 2016

From Help.

Activates an application window.

object.AppActivate title 

object WshShell object.

title Specifies which application to activate. This can be a string containing the title of the application (as it appears in the title bar) or the application's Process ID.

I don't know what access to info you have about the window. Some COM objects have a HWnd property. This post gets you how to convert a hwnd to a ProcessID to be used above.

How to find the window Title of Active(foreground) window using Window Script Host

This shows how to convert a process command-line to a ProcessID. To see what properties and methods are available use the command-line tool wmic (wmic process get /? and wmic process call /?)

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
    msgbox objItem.ProcessID & " " & objItem.CommandLine
Next