从 Excel 调用 SAP RFC

Call SAP RFC from Excel

我已经为 Windows 7.70(带有 64 位连接器的最新版本)下载了 SAP GUI,并使用 material 字符串导入参数创建了一个名为 Z_GF_STOCK 的 RFC 函数(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 配合得很好。