excelvbasap-gui

SAP Automation on multiple screens


I've a question regarding running VBA automation in SAP in two or more screen. Is it possible if the text hit "1" then the script need to use other SAP screen to run the automation.

 If session.findById("wnd[0]/usr/txtBKPF-MONAT").Text = "1" Then

'Run FB03 in new sap screen, number 2 or any active 2nd screen

I saw this video in YouTube but I couldn't find any respective guide for like this.

https://www.youtube.com/watch?v=PvLq11HfCTc

here is my current code.

If Not IsObject(SAPApp) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set SAPApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(SAPCon) Then
   Set SAPCon = SAPApp.Children(0)
End If
If Not IsObject(session) Then
   Set session = SAPCon.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject SAPApp, "on"
End If


    Dim rng As Range
    Dim cell As Range
    Dim cell2 As Range
    Dim rFirst As Range
    Dim rLast  As Range
    Dim rSource As Range
    Dim LastRow As Long


LastRow = ActiveWorkbook.ActiveSheet.Cells(Rows.Count, "E").End(xlUp).Row

For y = 12 To LastRow

Paymentdate = ActiveWorkbook.ActiveSheet.Range("E" & y).Value
'Period = Worksheets("Sheet1").Range("B3").Value
Paymentamt = ActiveWorkbook.ActiveSheet.Range("D" & y).Value
Assignment = ActiveWorkbook.ActiveSheet.Range("D7").Value

ActiveWorkbook.ActiveSheet.Range("H" & y).Formula = "=Trim(TRANSPOSE(TEXTSPLIT(G" & y & ",CHAR(44))))"

    Columns("H:H").Select
    Selection.Replace What:="@", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
Range("H" & y).Select

session.findById("wnd[0]/tbar[0]/okcd").Text = "/NF-28"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/sub:SAPMF05A:0103/radRF05A-XPOS1[2,0]").Select
session.findById("wnd[0]/usr/ctxtBKPF-BLDAT").Text = Paymentdate
session.findById("wnd[0]/usr/ctxtBKPF-BLART").Text = "DZ"
session.findById("wnd[0]/usr/ctxtBKPF-BUKRS").Text = "ES01"
session.findById("wnd[0]/usr/ctxtBKPF-BUDAT").Text = Paymentdate
session.findById("wnd[0]/usr/txtBKPF-MONAT").SetFocus
If session.findById("wnd[0]/usr/txtBKPF-MONAT").Text = "1" Then

'Run FB03 in new sap screen, number 2 or any active 2nd screen

Else

session.findById("wnd[0]/usr/ctxtBKPF-WAERS").Text = "EUR"
session.findById("wnd[0]/usr/ctxtRF05A-KONTO").Text = "115117"

Solution

  • After bunch of tries, finally I found the solution to run and choose the second SAP window screen by calling the function from below. Also can switch the automation to run on first SAP screen and next screen by changing few codes below.

    Dim sapSys2 As String
    Set session = getSAPStat2(sapSys2)
    

    so then it will be

    If session.findById("wnd[0]/usr/txtBKPF-MONAT").Text = "1" Then
        
    Dim sapSys2 As String
    Set session = getSAPStat2(sapSys2)
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/NFB03"
    'continue
    Else
    'continue
    

    Full function code :

        Function getSAPStat2(ByRef sapBindStatus2 As String) As Object
    
        On Error GoTo errored
    If Not IsObject(SAPApp) Then
       Set SapGuiAuto = GetObject("SAPGUI")
       Set SAPApp = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(SAPConnection) Then
       Set SAPCon = SAPApp.Children(0)
    End If
    
    session_number = 1
    Set session = SAPCon.Children(Int(session_number))
    
    If Not IsObject(session) Then
       Set session = SAPCon.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session, "on"
       WScript.ConnectObject SAPApp, "on"
    End If
    
        Set getSAPStat2 = session
        sapBindStatus2 = session.info.systemname
        Exit Function
    errored:
        sapBindStatus2 = "SAP session could not be accessed!"
    End Function