簡體   English   中英

從 Excel 調用 SAP RFC

[英]Call SAP RFC from Excel

我已經為 Windows 7.70(具有 64 位連接器的最新版本)下載了 SAP GUI,並創建了一個名為Z_GF_STOCK的 RFC function,其中包含材料字符串導入參數( ARTICULO )和labst導出參數( STOCK )。 我需要將此 RFC 與 Excel 與 VBA 與此代碼一起使用:

Dim retcd        As Boolean
Dim SilentLogon  As Boolean
Set logonControl = CreateObject("SAP.LogonControl.1")
Set objBAPIControl = CreateObject("SAP.Functions")
Set R3Connection = logonControl.NewConnection
R3Connection.Client = "100"
R3Connection.ApplicationServer = "192.168.XXX.XXX"
R3Connection.Language = "XX"
R3Connection.User = "XXXXX"
R3Connection.Password = "XXXXX"
R3Connection.System = "XXX"
R3Connection.SystemNumber = "XX"
R3Connection.UseSAPLogonIni = False
retcd = R3Connection.Logon(0, True)
objBAPIControl.Connection = R3Connection
Set objgetaddress = objBAPIControl.Add("Z_GF_STOCK")
objgetaddress.exports("ARTICULO") = "XXXXXX"

returnFunc = objgetaddress.Call

If returnFunc = True Then
    ActiveCell.Value = objgetaddress.imports("STOCK")
    objBAPIControl.Connection.Logoff
    R3Connection.Logoff
Else
    MsgBox "Error call Z_GF_STOCK! "
End If

objBAPIControl.Connection.Logoff
R3Connection.Logoff
R3Connection.Logoff

returnFunc變量始終為 false,並且從不提升行來獲取值。

我使用此代碼調用 FM

Sub DontCallMe_GaGA()
    Set R3 = CreateObject("SAP.Functions")
    Set myConnction = R3.Connection
    myConnction.ApplicationServer = "SYSTEM"
    myConnction.SystemNumber = 54
    myConnction.Client = "001"
    myConnction.user = "USER"
    myConnction.Password = "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
                aSheet.Cells(i, 1) = user(2)
                aSheet.Cells(i, 2) = user(3)
                aSheet.Cells(i, 3) = user(5)
                aSheet.Cells(i, 4) = user(16)
                i = i + 1
            Next
        Next
    Else
        MsgBox " Call Failed! error: "
    End If
    myConnction.logoff
End Sub

它與 SAP Gui 7.70 和 Office 365 配合使用就像一個魅力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM