Home > Enterprise >  Call SAP RFC from Excel
Call SAP RFC from Excel

Time:11-19

I've downloaded SAP GUI for Windows 7.70 (latest version with 64bits connectors) and created an RFC function called Z_GF_STOCK with a material string import parameter (ARTICULO) and labst export parameter (STOCK). I need to use this RFC with Excel with VBA with this code:

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

The returnFunc variable is always false and never raises the line to get the value.

CodePudding user response:

I use this code for calling FMs

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

It works like a charm with SAP Gui 7.70 and Office 365.

  • Related