Activating (bringing to the foreground) a specific window using vbscript

I don’t even know where to start with my question, I tried a hundred things and worked for several hours, but did not find anything useful. (I am open to every trick.)

Here is my problem:

I have a .hta combo file that looks like this:

hta file

It lists all the sessions / modifications of my SAP Gui.

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: if I double-click on one of the entries in this list, the selected instance of my SAP Gui should go to the forefront / activate.

Unfortunately, my task manager displays only one task, and this is "SAP Logon". One of my open windows also has the name "SAP Logon", all the others have the same name: "SAP Easy Access".

enter image description here

The only way to see connection identifiers (server name) and session identifiers is to extract them using vbscript. (see above)

Is there any way to do this? The only workarounds that I could come up with after a few thousand solutions are the following 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 and then maximizes the selected SAP window. Unfortunately, my HTA-GUI is also minimized, which kinda sucks.

Second idea:

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

By hand you need to do this:

Click on this little arrow, right-click on the icon, and then left-click on the name.

information tray symbols

Is there a way to automate this? It drives me crazy.

Hope someone can help me, this would be STRONGLY appreciated.

PS: I'm sitting on a machine with limited rights, so I may not be able to solve this problem using the Windows API-ish solutions.

EDIT regarding comments:

It's impossible:

  • modify registry entries
  • create COM objects
  • works with anything but VBScript
+5
source share
4 answers

I found him...

The resizeWorkingPane method - for resizing a window - also works on windows in the background. If you change the settings, the window will come to the fore.

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

I need to partially undo this because it does not work in all windows. I still don't know why, but sometimes it fails. However, it seems to me that this is the closest thing you can get.

+1
source

From the reference.

Activates the application window.

 object.AppActivate title 

object A WshShell object.

name Indicates which application to activate. This can be a string containing the application title (as indicated in the title bar) or an application process identifier.

I do not know what access to the information you have about the window. Some COM objects have the HWnd property. In this post, you will learn how to convert hwnd to ProcessID, which will be used above.

Finding an Active Window (Front End) Window Name Using Window Script Host

Yhis shows how to convert the command line of a process to ProcessID. To find out what properties and methods are available, use the wmic command line tool ( 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 
+1
source

Similarly, it also works with the following commands:

 session.findById("wnd[0]").iconify session.findById("wnd[0]").maximize 
+1
source

This is 100% of the time. It is ugly, but it works. You can change the IQS3 t code to any other that you can confirm that the user will not and will have access. Also part of my reasoning for choosing this code is that it loads quickly.

 Set objShell = CreateObject("wscript.shell") session.findById("wnd[0]/tbar[0]/okcd").text = "/nIQS3" session.findById("wnd[0]").sendVKey 0 objShell.AppActivate(cstr(session.ActiveWindow.Text)) session.findById("wnd[0]/tbar[0]/btn[3]").press 
0
source

Source: https://habr.com/ru/post/1242304/


All Articles