Search code examples
windowsvbscriptsap-gui

Activating (bring to foreground) a specific window with vbscript


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 GUI 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

Solution

  • I found it...

    The resizeWorkingPane method - for changing the size of a window - also works on windows in the background. If you change the parameters, the window will come to the foreground.

    session.findById("wnd[0]").resizeWorkingPane 300,200,false
    

    I have to partially revoke this, because it doesnt work on all windows. I'm still not sure why, but it keeps failing sometimes. Still, it seems to me, that this is the closest you can get.