cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Script in Macro sometimes work, sometimes don't :(

Former Member
0 Kudos

Hi Script Man/ Everyone,

I've been using SAP recording and getting the codes to input in an excel Macro. I want some fields in SAP to pick up the values in Excel. My code is as follows:

Sub Test_Settlement()

'

' Settlement Macro

'

'

Dim ws As Object

Set ws = CreateObject("Wscript.Shell")

Dim URL1 As String, URL2 As String, URL_R3 As String

URL1 = "https://sww-ds.europe.shell.com/irj/servlet/prt/portal/prtroot/pcd!3aportal_content!2fevery_user!2fg..."

URL2 = "&ExecuteLocally=true&CurrentWindowId=WID1423478463738&supportInitialNavNodesFilter=true&PrevNavTarget=navurl%3A%2F%2Fbe6f59d187a186d43b9880ff80410877&NavMode=1"

URL_R3 = "%3A%2F%2F6e65616b48579aa29c19725ad666f27d"

R3 = URL1 & URL_R3 & URL2

ws.Run R3

start_connection:

On Error GoTo err_handler:

connection_id = 0

If Not IsObject(applicationSAP) Then

   Set SAPGUIAUTO = GetObject("SAPGUI")

   Set applicationSAP = SAPGUIAUTO.GetScriptingEngine

End If

connection_id = applicationSAP.Children.Count

If Not IsObject(connection) Then

   Set connection = applicationSAP.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 applicationSAP, "on"

End If

On Error Resume Next

session.findById("wnd[0]").resizeWorkingPane 133, 24, False

session.findById("wnd[0]/tbar[0]/okcd").Text = "KO8G"

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

session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = "OP01"

session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").caretPosition = 4

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

session.findById("wnd[0]/usr/chkLKO74-BATCH").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TESTLAUF").Selected = True

session.findById("wnd[0]/usr/chkLKO74-LIST").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").Selected = True

session.findById("wnd[0]/usr/subBLOCK1:SAPLKASS:0100/ctxtCODIA-VARIANT").Text = "ZFSSM_00"

session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = Cells(2, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = Cells(3, 2).Value

session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = Cells(4, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = Cells(5, 2).Value

session.findById("wnd[0]/usr/cmbLKO74-VAART").Key = "1"

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").SetFocus

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

session.findById("wnd[0]/usr/txtKABA01-JNAME").Text = "00 EARLY RUN"

session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").Text = "parallel_generators"

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STDAY").Text = "23.02.2016"

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STTME").Text = "23:00:00"

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STTME").SetFocus

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STTME").caretPosition = 8

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

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

session.findById("wnd[0]/tbar[0]/okcd").Text = "/NCJ8G"

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

session.findById("wnd[0]/usr/chkLKO74-BATCH").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TESTLAUF").Selected = True

session.findById("wnd[0]/usr/chkLKO74-LIST").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").Selected = True

session.findById("wnd[0]/usr/subBLOCK1:SAPLKAOP:0500/ctxtPRZB-VARIANT").Text = "ZFSSM_01"

session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = Cells(2, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = Cells(3, 2).Value

session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = Cells(4, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = Cells(5, 2).Value

session.findById("wnd[0]/usr/cmbLKO74-VAART").Key = "1"

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").SetFocus

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

session.findById("wnd[0]/usr/txtKABA01-JNAME").Text = "01 EARLY RUN"

session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").Text = "parallel_generators"

session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").SetFocus

session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").caretPosition = 19

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpNJOB").Select

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpNJOB/ssubKABA01_SUBSN:SAPLKABA:0212/ctxtKABA01-PRJOB").Text = "00 EARLY RUN"

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpNJOB/ssubKABA01_SUBSN:SAPLKABA:0212/ctxtKABA01-PRJOB").caretPosition = 22

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

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

Err.Clear

On Error GoTo 0

Exit Sub

err_handler:

Err.Clear

On Error GoTo 0

Resume start_connection:

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

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

End Sub

Our SAP identity is embedded in our computer profile and i only got the opening script from another colleague. However, it pops a new window, opens another session and pop up asks if "A script is trying to attach to the GUI" then all the script runs. However, when i rerun the Macro, the pop up does not show anymore and the script only opens SAP without the transaction codes and no executions.

I also put the On Error Resume Next because of this script:

session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = "OP01"

session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").caretPosition = 4

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

which is only applicable on first log on.

Hoping for your help.

My advance thanks.

Accepted Solutions (0)

Answers (1)

Answers (1)

script_man
Active Contributor
0 Kudos

Hi Kevin,

I would proceed as follows:

1. The Information "A script is trying to attach to the GUI" suggests that one should first disable it.

for example as follows:

SAP GUI -> Alt / F12 (local layout tune) -> Options -> Scripting:-
Notify when a script connects to SAP GUI -> deactivate-
Notify when a script establishes a connection -> deactivate
2. In the design of the code with error_handler even little things can become important. Therefore, I would disable all commands with "caret position". These are mostly unnecessary.
for example as follows:
'session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpNJOB/ssubKABA01_SUBSN:SAPLKABA:0212/ctxtKABA01-PRJOB").caretPosition = 22
Then one would have to try again.
Regards,
ScriptMan
Former Member
0 Kudos

Hi Script Man,

Wheew!!! Thanks for the prompt reply. No. 1 was a great help. It felt like it's flowing all smoothly now. However, i can't seem to remove the caret position, if disable means to remove (sorry, got beginner's knowledge). SAP can't seem to determine which fields the text needs to be inputted especially that the destination is a pop up window (wnd[1]).

Ahmnn... can you suggest what is missing in my script so that it executes the script on the window that it opens? Currently, if another SAP GUI is open, it is where the script executes and leaves the opened SAP GUI behind.

Thanks again and i am learning from your posts and answers.

Former Member
0 Kudos

I searched more and figured out caret position appears based on my way of recording. I am not sure but it seems to appear when i press "Tab" on the keyboard so i tried recording without it. It still shows setfocus but only shows not the caret position at the end of the script and is set to "00"

I now get what you mean.

Thanks. ^_^

Former Member
0 Kudos

I saw this code which opens a new session if no session is avaliable but i can't make it work to run together with my code:

'Function cmdattachx() As Object

  Session_Nr = -1

  Set GuiAuto = GetObject("SAPGUI")

  Set sapapplication = GuiAuto.GetScriptingEngine

  For Each connection In sapapplication.Children

    If Not connection.DisabledByServer Then

    session_number_all = connection.Children.Count

      For Each session In connection.Children

        If session.Busy = False Then

          If session.info.Transaction = "SESSION_MANAGER" Then

            Session_Nr = session.info.sessionnumber - 1

            Set session = connection.Children(Int(Session_Nr))

            Hwnd = session.ActiveWindow.Handle

            session.ActiveWindow.SetFocus

            session.TestToolMode = 1

            Set cmdattachx = session

            Exit Function

          End If

        End If

      Next

    End If

  Next

    If session_number_all = 6 And Session_Nr = -1 Then

        MsgBox "Maximum number of sessions reached"

        Exit Function

    End If

  If Session_Nr = -1 Then

    For Each connection In sapapplication.Children

      If Not connection.DisabledByServer Then

        For Each session In connection.Children

          If session.Busy = False Then

            session.createsession

            sessionFound = True

            Exit For

          End If

        Next

        If sessionFound = True Then

          Exit For

        End If

      End If

    Next

  End If

End Function

Sub Test()

  Set session = cmdattachx()

  If session Is Nothing Then

    Application.Wait (Now + TimeValue("0:00:02"))

    Set session = cmdattachx()

  End If'

script_man
Active Contributor
0 Kudos

Hi Kevin,

I can only guess you would have to do the following.

for example:

Sub Test_Settlement()

'

' Settlement Macro

'

'

Dim ws As Object

Set ws = CreateObject("Wscript.Shell")

Dim URL1 As String, URL2 As String, URL_R3 As String

URL1 = "https://sww-ds.europe.shell.com/irj/servlet/prt/portal/prtroot/pcd!3aportal_content!2fevery_user!2fg..."

URL2 = "&ExecuteLocally=true&CurrentWindowId=WID1423478463738&supportInitialNavNodesFilter=true&PrevNavTarget=navurl%3A%2F%2Fbe6f59d187a186d43b9880ff80410877&NavMode=1"

URL_R3 = "%3A%2F%2F6e65616b48579aa29c19725ad666f27d"

R3 = URL1 & URL_R3 & URL2

ws.Run R3, 1, true

start_connection:

Set session = cmdattachx()

If session Is Nothing Then

   Application.Wait (Now + TimeValue("0:00:03"))

   Set session = cmdattachx()

End If


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

session.findById("wnd[0]/tbar[0]/okcd").Text = "KO8G"

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

session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = "OP01"

'session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").caretPosition = 4

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

session.findById("wnd[0]/usr/chkLKO74-BATCH").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TESTLAUF").Selected = True

session.findById("wnd[0]/usr/chkLKO74-LIST").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").Selected = True

session.findById("wnd[0]/usr/subBLOCK1:SAPLKASS:0100/ctxtCODIA-VARIANT").Text = "ZFSSM_00"

session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = Cells(2, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = Cells(3, 2).Value

session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = Cells(4, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = Cells(5, 2).Value

session.findById("wnd[0]/usr/cmbLKO74-VAART").Key = "1"

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").SetFocus

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

session.findById("wnd[0]/usr/txtKABA01-JNAME").Text = "00 EARLY RUN"

session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").Text = "parallel_generators"

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STDAY").Text = "23.02.2016"

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STTME").Text = "23:00:00"

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STTME").SetFocus

'session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpDATE/ssubKABA01_SUBSN:SAPLKABA:0211/ctxtKABA01-STTME").caretPosition = 8

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

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

session.findById("wnd[0]/tbar[0]/okcd").Text = "/NCJ8G"

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

session.findById("wnd[0]/usr/chkLKO74-BATCH").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TESTLAUF").Selected = True

session.findById("wnd[0]/usr/chkLKO74-LIST").Selected = True

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").Selected = True

session.findById("wnd[0]/usr/subBLOCK1:SAPLKAOP:0500/ctxtPRZB-VARIANT").Text = "ZFSSM_01"

session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = Cells(2, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = Cells(3, 2).Value

session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = Cells(4, 2).Value

session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = Cells(5, 2).Value

session.findById("wnd[0]/usr/cmbLKO74-VAART").Key = "1"

session.findById("wnd[0]/usr/chkLKO74-TDCHECK").SetFocus

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

session.findById("wnd[0]/usr/txtKABA01-JNAME").Text = "01 EARLY RUN"

session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").Text = "parallel_generators"

session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").SetFocus

'session.findById("wnd[0]/usr/ctxtKABA01-RFCGR").caretPosition = 19

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpNJOB").Select

session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpNJOB/ssubKABA01_SUBSN:SAPLKABA:0212/ctxtKABA01-PRJOB").Text = "00 EARLY RUN"

'session.findById("wnd[0]/usr/tabsKABA01_TBSTR/tabpNJOB/ssubKABA01_SUBSN:SAPLKABA:0212/ctxtKABA01-PRJOB").caretPosition = 22

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

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

End Sub

Regards,

ScriptMan