使用VBA进行SAP录屏

Option Explicit

Private Function SystemSession(SapApp As Object) As Object
    
    Dim oGuiSession As Object, k As Long
    For Each oGuiSession In SapApp.Children
        
        If InStr(oGuiSession.ConnectionString, "###.###.###.###") > 0 Then
            
            Set SystemSession = oGuiSession
            If SystemSession.Children.count > 1 Then
                MsgBox "Please keep one SAP session."
                Set SystemSession = Nothing
                Exit Function
            End If
            
            Set SystemSession = oGuiSession.Children(0)
            
            If SystemSession.Info.User = "" Then
                MsgBox "Please login SAP first.", vbInformation
                Set SystemSession = Nothing
                Exit Function
            End If
            
        End If
        
    Next
    
End Function

Public Function GetSapSession() As Object
    
    Dim SapGuiAuto As Object
    Dim SapApp As Object
    Dim sapSession As Object
    
    Set sapSession = Nothing
    
    On Error Resume Next
    If SapGuiAuto Is Nothing Then
        Set SapGuiAuto = GetObject("SAPGUI")
    End If
    On Error GoTo 0
    
    If SapGuiAuto Is Nothing Then
        MsgBox "Please open sap first!"
        GoTo FunFinal:
    End If
    
    If SapApp Is Nothing Then
        Set SapApp = SapGuiAuto.GetScriptingEngine
    End If
    
    If SapApp.Children.count = 0 Then
        MsgBox "please login server"
        GoTo FunFinal:
    End If
    
    Set sapSession = SystemSession(SapApp)
    
    
FunFinal:
    
    Set GetSapSession = sapSession
    
End Function


Sub Test()
    
    
    Dim Session As Object
    
    Set Session = GetSapSession()
    If Session Is Nothing Then
        Exit Sub
    End If
    
    录屏逻辑......   
    
End Sub

 

暂无评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注