cancel
Showing results for 
Search instead for 
Did you mean: 

How to use SAP script to open a new window and then interact with the new window

fabian_v
Explorer
0 Kudos

Hello,

I've recorded my first script, but there's a catch: it only functions correctly when I have the SAP Easy Access menu open and no other windows or transactions.

To address this, I added "/o" before the MM03 transaction command to open it in a new window.

While the transaction opens fine in the new window, SAP continues to input data in the original window instead of the new one. I'd like the script to open MM03 in a new window using "/o" and then continue operations in that new window. This way, it won't matter if I'm in the SAP Easy Access menu when starting the script. Unfortunately, I haven't found a solution for this yet.

Any insights or recommendations would be greatly appreciated!

Here's my current code (saved as a .vbs script):

If Not IsObject(application) Then
   Set SapGuiAuto  = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.Children(0)
End If
If Not IsObject(session) Then
   Set session    = connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session,     "on"
   WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").resizeWorkingPane 120,40,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/oMM03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "132132"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0

Accepted Solutions (0)

Answers (1)

Answers (1)

FrankKrauseGUI
Advisor
Advisor

Hello Fabian,

the window that will execute the scripting commands is specified within the ID part of "session.findById".

One by one:
session.findById("wnd[0]/tbar[0]/okcd").text = "/oMM03"
-> Enters the text "/oMM03" in the OK Code field of the first window belonging to the session. This window is called wnd[0].

session.findById("wnd[0]").sendVKey 0
-> Sends the virtual key for ENTER in the same window.
-> This will start the transaction in a new window which will have ID 1 (and not 0), so this will be wnd[1].

session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "132132"
-> This again works on the first window in which MM03 has not been started and that is the error.
You need to use wnd[1] here, like:
session.findById("wnd[1]/usr/ctxtRMMG1-MATNR").text = "132132"

You probably assembled your script from two individual recordings and did not adjust the window id.

Best regards,
Frank

fabian_v
Explorer
0 Kudos

Hello Frank,

thank you for your answer.

You are right - I actually changed the script but in a different way. The original script was:

End If
session.findById("wnd[0]").resizeWorkingPane 120,40,false
session.findById("wnd[0]/tbar[0]/okcd").text = "MM03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "132132"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0

This code worked perfectly, so I assumed if I would open MM03 with /o I'd have a new window in wich it would just work as well.

I followed your advice and edited the code to this:

session.findById("wnd[0]").resizeWorkingPane 120,40,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/oMM03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/usr/ctxtRMMG1-MATNR").text = "132132"
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0

Unfortunately it still does not work, it opens MM03 in a new window but then fails to enter the Number as well as sending "ENTER". It seems like SAP does not realize that a new window is opened and still tries to enter the data in the original window.

Thank you

FrankKrauseGUI
Advisor
Advisor

Hello Fabian,

yes, it cannot work as I told you it would, because a main window is of course never a wnd[1]. The 1..n is a hierarchy level for modal windows (popups).

What you need to do after opening the new window is to find the right session.

Like this:
Set session2 = connection.Children(1)

And afterwards, you can access the main window again via wnd[0], but for session2.

Best regards,
Frank

fabian_v
Explorer
0 Kudos

Hi Frank,

thank you! This explains a lot!

I now updated the code to the following:

[...]
session.findById("wnd[0]").resizeWorkingPane 120,40,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/oMM03"
session.findById("wnd[0]").sendVKey 0
Set session2 = connection.Children(1)
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "132132"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0

But now it shows this error-message:

"The enumerator of the collection cannot find an element with the specified index."

I'm sorry that I'm causing you so much trouble, but I'm very grateful for your help!

FrankKrauseGUI
Advisor
Advisor

session2 not session in this code...

session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "132132"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
fabian_v
Explorer
0 Kudos

Thank you! I changed it to:

End If
session.findById("wnd[0]").resizeWorkingPane 120,40,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/oMM03"
session.findById("wnd[0]").sendVKey 0
Set session2 = connection.Children(1)
session2.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "1570656"
session2.findById("wnd[0]").sendVKey 0
session2.findById("wnd[1]").sendVKey 0
session2.findById("wnd[1]").sendVKey 0

But it keeps showing the following error-message: "The enumerator of the collection cannot find an element with the specified index."