cancel
Showing results for 
Search instead for 
Did you mean: 

Script recording & playback, save as and new session

Former Member
0 Kudos

I am trying to create a script using the script recording and playback function. The script is supposed to run three separate transactions, two in separate sessions. It runs the first transaction MB51 and exports to excel. I cannot get the save as function to process transparently. It is a temporary file so I would like to just overwrite without user input dialog. The second transaction runs with the /* parameter and works fine. The third transaction I am trying to run in a new session. I use the /o parameter and it will create the new session. When I playback the script it just ends at the last transaction screen without executing. I do not receive any error message from SAP.

Generated script below

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]").maximize

session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb51"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/btn%_MATNR_%_APP_%-VALU_PUSH").press

session.findById("wnd[1]/tbar[0]/btn[16]").press

session.findById("wnd[1]/tbar[0]/btn[24]").press

session.findById("wnd[1]/tbar[0]/btn[8]").press

session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = "2110"

session.findById("wnd[0]/usr/ctxtWERKS-HIGH").setFocus

session.findById("wnd[0]/usr/ctxtWERKS-HIGH").caretPosition = 0

session.findById("wnd[0]/tbar[1]/btn[8]").press

session.findById("wnd[0]/tbar[1]/btn[48]").press

session.findById("wnd[0]/mbar/menu[0]/menu[3]").select

session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[1]").select

session.findById("wnd[0]/tbar[0]/okcd").text = "/*mb52"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]").resizeWorkingPane 128,61,false

session.findById("wnd[0]/tbar[0]/okcd").text = "/omb58"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]").resizeWorkingPane 128,61,false

Accepted Solutions (0)

Answers (1)

Answers (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Hugh,

welcome in this forum and at SAP GUI Scripting.

After you open a new session via /omb58 you must detect the session. It is not possible to use via session.findById("wnd[0]") another session. Here an example how to loop over the open sessions of one connection to find one:

'-Begin-----------------------------------------------------------------
  Sub Main

    Set SapGuiAuto = GetObject("SAPGUI")
    If Not IsObject(SapGuiAuto) Then
      Exit Sub
    End If

    Set SapAppl = SapGuiAuto.GetScriptingEngine
    If Not IsObject(SapAppl) Then
      Exit Sub
    End If

    Set Conn = SapAppl.Children(0)
    If Not IsObject(Conn) Then
      Exit Sub
    End If
   
    Set CollSes = Conn.Sessions()
    If Not IsObject(CollSes) Then
      Exit Sub
    End If 
      
    '-Loop over sessions------------------------------------------------
      For j = 0 To CollSes.Count() - 1
     
        Set Sess = Conn.Children(CLng(j))
        If Not IsObject(Sess) Then
          Exit Sub
        End If 

        If Sess.Busy() = vbFalse Then
       
          Set SessInf = Sess.Info()
          If IsObject(SessInf) Then

              Trans = SessInf.Transaction()

              '-If transaction code is MB58 do your activities----------
              If Trans = "MB58"  Then

                '-In my case I open a message box with the title of the session
                MsgBox Sess.findById("wnd[0]/titl").Text

              End If

          End If
           
        End If

      Next

  End Sub

  '-Main----------------------------------------------------------------
    Main

'-End-------------------------------------------------------------------

In this case I search for the transaction code MB58 and on this way I detect the correct session.

You can finde more information here.

Cheers
Stefan

Former Member
0 Kudos

Stefan, thank you for the solution you supplied. I think I understand the process, but I think I do not have the syntax correct. I copied and pasted the code from the record and playback script into your code. I have managed to not get any errors, but I am getting the same results. The last transaction opens in a new window but does not process. Thank you again for your patience as I am not a programmer.


'-Begin-----------------------------------------------------------------

Sub Main

Set SapGuiAuto = GetObject("SAPGUI")

If Not IsObject(SapGuiAuto) Then

Exit Sub

End If

Set SapAppl = SapGuiAuto.GetScriptingEngine

If Not IsObject(SapAppl) Then

Exit Sub

End If

Set Conn = SapAppl.Children(0)

If Not IsObject(Conn) Then

Exit Sub

End If

Set CollSes = Conn.Sessions()

If Not IsObject(CollSes) Then

Exit Sub

End If

'-Loopoversessions------------------------------------------------

Forj=0ToCollSes.Count() -1

SetSess=Conn.Children(CLng(j))

IfNotIsObject(Sess) Then

ExitSub

EndIf

IfSess.Busy() =vbFalseThen

SetSessInf=Sess.Info()

IfIsObject(SessInf) Then

Trans=SessInf.Transaction()

'-If transaction code is MB58 do your activities----------

If Trans = "MB58" Then

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

session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = ""

session.findById("wnd[0]/usr/btn%_MATNR_%_APP_%-VALU_PUSH").press

session.findById("wnd[1]/tbar[0]/btn[16]").press

session.findById("wnd[1]/tbar[0]/btn[24]").press

session.findById("wnd[1]/tbar[0]/btn[8]").press

session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = "2110"

session.findById("wnd[0]/usr/ctxtWERKS-LOW").caretPosition = 4

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/tbar[1]/btn[8]").press

'-InmycaseIopenamessageboxwiththetitleofthesession

MsgBoxSess.findById("wnd[0]/titl").Text

EndIf

EndIf

EndIf

Next

EndSub

'-Main----------------------------------------------------------------

Main

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]").maximize

session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb52"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/btn%_MATNR_%_APP_%-VALU_PUSH").press

session.findById("wnd[1]/tbar[0]/btn[16]").press

session.findById("wnd[1]/tbar[0]/btn[24]").press

session.findById("wnd[1]/tbar[0]/btn[8]").press

session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = "2110"

session.findById("wnd[0]/usr/ctxtWERKS-HIGH").setFocus

session.findById("wnd[0]/usr/ctxtWERKS-HIGH").caretPosition = 0

session.findById("wnd[0]/tbar[1]/btn[8]").press

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

session.findById("wnd[0]/tbar[0]/okcd").text = "/omb58"

session.findById("wnd[0]").sendVKey 0

'-End-------------------------------------------------------------------

stefan_schnell
Active Contributor
0 Kudos

Hello Hugh,

try this:

'-Begin-----------------------------------------------------------------
  Sub Main

    Set SapGuiAuto = GetObject("SAPGUI")
    If Not IsObject(SapGuiAuto) Then
      Exit Sub
    End If

    Set SapAppl = SapGuiAuto.GetScriptingEngine
    If Not IsObject(SapAppl) Then
      Exit Sub
    End If

    Set Conn = SapAppl.Children(0)
    If Not IsObject(Conn) Then
      Exit Sub
    End If
   
    Set CollSes = Conn.Sessions()
    If Not IsObject(CollSes) Then
      Exit Sub
    End If 
      
    '-Loop over sessions------------------------------------------------
      For j = 0 To CollSes.Count() - 1
     
        Set Sess = Conn.Children(CLng(j))
        If Not IsObject(Sess) Then
          Exit Sub
        End If 

        If Sess.Busy() = vbFalse Then
       
          Set SessInf = Sess.Info()
          If IsObject(SessInf) Then

              Trans = SessInf.Transaction()

              If Trans = "MB58"  Then

                Sess.findById("wnd[0]").maximize
                Sess.findById("wnd[0]/usr/ctxtWERKS-LOW").text = ""
                Sess.findById("wnd[0]/usr/btn%_MATNR_%_APP_%-VALU_PUSH").press
                Sess.findById("wnd[1]/tbar[0]/btn[16]").press
                Sess.findById("wnd[1]/tbar[0]/btn[24]").press
                Sess.findById("wnd[1]/tbar[0]/btn[8]").press
                Sess.findById("wnd[0]/usr/ctxtWERKS-LOW").text = "2110"
                Sess.findById("wnd[0]/usr/ctxtWERKS-LOW").caretPosition = 4
                Sess.findById("wnd[0]").sendVKey 0
                Sess.findById("wnd[0]/tbar[1]/btn[8]").press

              End If

          End If
           
        End If

      Next

  End Sub

  '-Main----------------------------------------------------------------
    Main

'-End-------------------------------------------------------------------

Cheers
Stefan