cancel
Showing results for 
Search instead for 
Did you mean: 

VBA program executing SAP RFCs works in Excel 2016 on-prem, but not in Office 365

mburnett
Explorer
0 Kudos
3,562

My company is making the move to Office 365 soon. I tested a program that logs into SAP and runs RFCs in the 365 environment and I'm getting a run-time error on the line `Functions.Connection = objConnection` (Run-time error 20080008…Bad Variant Type)

Option Explicit
    Public Functions As SAPFunctionsOCX.SAPFunctions
    Private LogonControl As SAPLogonCtrl.SAPLogonControl
    Private objConnection As SAPLogonCtrl.Connection
    Public Func As SAPFunctionsOCX.Function
    Public Commit As SAPFunctionsOCX.Function
    Public TableFactory As SAPTableFactory
    Public silentLogon As Boolean
    Public tblReadTableOptions, tblReadTableFields, tblReadTableData As SAPTableFactoryCtrl.Table
    Sub ExtractProjectData()
        If objConnection Is Nothing Then LogonToSAP
        InitiateSAPVariables
        Set Func = Functions.Add("BBP_RFC_READ_TABLE")
        Set tblReadTableOptions = Func.Tables("OPTIONS")
        Set tblReadTableFields = Func.Tables("FIELDS")
        Set tblReadTableData = Func.Tables("DATA")
        'extract/transform data from SAP tables
    End Sub
    Function InitiateSAPVariables()
        Set Functions = Nothing
        Set TableFactory = Nothing
        Set Func = Nothing
        Set Functions = CreateObject("SAP.Functions")
        Set TableFactory = CreateObject("SAP.TableFactory.1")
        Functions.Connection = objConnection 'run-time error here in Office 365 but not in on-prem
    End Function
    Function LogonToSAP()
        Dim establishConnection As Boolean
        silentLogon = false
        Set LogonControl = CreateObject("SAP.LogonControl.1")
        Set objConnection = LogonControl.NewConnection
        objConnection.Client = "###"
        objConnection.Language = "EN"
        objConnection.SystemNumber = "##"
        objConnection.User = ""
        objConnection.Password = ""
        objConnection.HostName = "###############"
        objConnection.System = "###"
        objConnection.ApplicationServer = "###.###.#.##"
        establishConnection = objConnection.Logon(0, silentLogon)
    End Function

A quick check of objConnection tells me that logon was successful...so I know that part is working on 365. For some reason though, it doesn't like assigning the `Connection` property of the `Functions` SAPFunctionsOCX.SAPFunctions object in the 365 environment (please feel free to correct my verbiage on that...I know it's not quite right).

Note that I'm not seeing any reference issues nor am I getting any compile errors in either environment. The first sign of trouble is on execution of `Functions.Connection = objConnection`

There's one more twist here and that is that I have another older VBA program that logs into SAP and runs remote function calls that doesn't use SAPFunctionsOCX.SAPFunctions, but rather declares variable R3 as `Public R3 As Object` and then sets R3 later in the logon code as `Set R3 = CreateObject("SAP.Functions")`...it does not use OCX. In other words, the old routine uses late binding. When the Functions object (R3 in this case) is set this way, I am able to run RFCs in both on prem and Office 365 environments.

Function LogonProdSAP(Optional SuppressLoginScreen As Boolean)
        Application.ScreenUpdating = False
        '**********************************************
        'Create Server object and Setup the connection for DEV
        '**********************************************
        Set R3 = CreateObject("SAP.Functions")
        If SuppressLoginScreen Then
            R3.Connection.System = "###"
            R3.Connection.HostName = "###################"
            R3.Connection.SystemNumber = "##"
            R3.Connection.Client = "###"
            R3.Connection.User = "##########"
            R3.Connection.Password = "#########"
            R3.Connection.Language = "EN"
    '        Call Logger("LogonProdSAP>  " & GetUserName)
        End If
        LogonProdSAP = R3.Connection.logon(0, SuppressLoginScreen)
        If LogonProdSAP <> True Then MsgBox ("Logon error"): Exit Function
    End Function

I could just go back to doing it this way, but I'd rather not have to reconfigure all of the code I just set up. In addition, I prefer binding early so Intellitype works to show all properties/methods available to that object. I'm sure there are other benefits as well.

What do I have to do to get the early-binding technique to work on Office 365?

This question was also posted on stack overflow...https://stackoverflow.com/questions/58942674

Accepted Solutions (0)

Answers (1)

Answers (1)

0 Kudos

Hello Michael,

Unfortunately I can't help with your issue, but I'm interested in the idea of executing SAP RFC through Excel VBA. Does it allow to read SAP table data and export it to Excel in a background?

Thank you

Best Regards,

Michal Kaczmarczyk

mburnett
Explorer
0 Kudos

Yes. I use BBP_RFC_READ_TABLE for this purpose. As you may already know, you'll need to get your role changed to be able to test/run this and other BAPIs, which run everything in the background (no GUI scripting). You can populate your Excel sheet by iterating through the data in the results tables produced by the BAPI. The learning curve isn't too steep, but it does take some getting used to. Your "immediate" and "locals" windows in the VB editor will come in very handy to figure out where your data is held.

This video should help you get started with running BAPIs from VBA. It doesn't demo table data extraction, but the principles are the same.

https://www.youtube.com/watch?v=QTq7TBTlV-A&lc=z22wdngpmrfjxjew204t1aokgzvpvnmyiegpaoekfyckbk0h00410...