使用Excel調用ABAP系統的函數

效果:在excel裏建立一個按鈕,開發一些VB script,能夠鏈接指定的ABAP系統並執行系統裏的ABAP function module。函數

在這裏例子裏執行ABAP系統的函數TH_USER_LIST, 把當前系統登陸的用戶取回來顯示在excel裏。spa

該按鈕的點擊響應函數實現代碼:excel

Sub CommandButton1_Click()
    Set R3 = CreateObject("SAP.Functions")
    Set myConnction = R3.Connection
    myConnction.ApplicationServer = "ag3.xxx.sap.corp"
    myConnction.SystemNumber = 54
    myConnction.Client = "001"
    myConnction.user = "WANGJER"
    myConnction.Password = "your password"
    
    If myConnction.Logon(0, True) <> True Then
      MsgBox "Logon failed"
      Exit Sub
    End If
    
    Dim callFunctionModule As Object
        
    Set callFunctionModule = R3.Add("TH_USER_LIST")
    callFunctionModule.Call

    If callFunctionModule.Exception <> "" Then
        MsgBox callFunctionModule.Exception
    End If

    If callFunctionModule.Call = True Then
        Dim result As Object
        Set result = callFunctionModule.tables("USRLIST")
        Dim aSheet As Worksheet
        Dim sheetCol As New Collection
        sheetCol.Add ActiveWorkbook.Sheets(1)
        For Each aSheet In sheetCol
            Dim i As Integer
            i = 1
            For Each user In result.Rows
                
                Client = user(2)
                UserName = user(3)
                Terminal = user(5)
                IP = user(16)
                aSheet.Cells(i, 1) = Client
                aSheet.Cells(i, 2) = UserName
                aSheet.Cells(i, 3) = Terminal
                aSheet.Cells(i, 4) = IP
                
                i = i + 1
            Next
        Next
    Else
        MsgBox " Call Failed! error: "
    End If
'log off
    myConnction.logoff
End Sub
相關文章
相關標籤/搜索