SAP RPA系列教程(八)

Sub SessionElementInitialization()

'对界面元素的值进行初始化,文本框值置为空,复选框状态改为【未勾选】

Dim UserArea As Object

    If Not IsObject(SAPApplication) Then
       Set SapGuiAuto = GetObject("SAPGUI")
       Set SAPApplication = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
       Set Connection = SAPApplication.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 SAPApplication, "on"
    End If
    Set UserArea = session.findByID("wnd[0]/usr")

    Clear_Fields UserArea
    MsgBox "已完成!"

End Sub

'_______________________________

Sub Clear_Fields(Area As Object)
    Set SAPApplication = GetObject("SAPGUI").GetScriptingEngine
    Dim Obj As Object
    Dim NextArea As Object
    On Error Resume Next
    For i = 0 To Area.Children.Count - 1
        Set Obj = Area.Children(CInt(i))
        If Obj.ContainerType = True Then
            If Obj.Children.Count > 0 Then
                Set NextArea = SAPApplication.findByID(Obj.ID)
                Clear_Fields NextArea
            End If
        End If
        If Obj.Type Like "*TextField*" And Obj.changeable = True Then
            Obj.Text = ""
        ElseIf Obj.Type = "GuiCheckBox" Then
            Obj.Selected = False
        End If
    Next i
End Sub

Excel Python 脚本执行

Sub RunPythonScript()

    '需要指定脚本文件中其他的根目录
    ChDir "C:\Users\poma\Documents\py4sap\rapsap"
    
    '定义对象
    Dim objShell As Object
    
    Dim PythonExePath, PythonScriptPath As String
    
    Set objShell = VBA.CreateObject("Wscript.Shell")
    
    'python的安装路径
    PythonExePath = """C:\Program Files\Python310\python.exe"""
    
    '执行文件的路径
    PythonScriptPath = "C:\Users\poma\Documents\py4sap\rapsap\rapsap.py"
    
    '0表示后台执行
    objShell.Run PythonExePath & PythonScriptPath, 0

End Sub