Skip to Content

SAP.Functions --- Active X component error

Hi All,

I have been trying to write a script where in i call a Function module from script and execute. I went through few forums for code. However, when i am using that i am getting error.

Please find below code and error.

Set funcControl = CreateObject("SAP.Functions")

Set compname = funcControl.Add("TMP_GUI_GET_COMPUTERNAME")

if compname.Call = True Then

computername = compname.IMPORTS("COMPUTERNAME")

MsgBox computername

End If

When i try to execute above code, i get error for Active X component can't create Object "SAP.Functions".

I was able to run the script with the help from one of the answers from Stefan (using 32-bit) cmd.exe.

Now the problem is, the call to program is not successful.

Set funcControl = CreateObject("SAP.Functions")

Set compname = funcControl.Add("TMP_GUI_GET_COMPUTERNAME")

MsgBox compname (This line gives FM name as output in message box)

if compname.Call = True Then

MsgBox "Call Successful"

Else

MsgBox "Call Not Successful"

End If

I am getting Call Not Successful everytime i try to run my script. Can you pls help me to understand why the call to program is not getting through?

Regards,

Dipesh

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Sep 27, 2017 at 07:49 PM

    Hello Dipesh,

    it is necessary to connect an SAP system before you invoke a function. Take a look at this VBS example with the function module RFC_PING:

    '-Begin-----------------------------------------------------------------
    
      '-Directives----------------------------------------------------------
      Option Explicit
    
      Sub Main() '----------------------------------------------------------
    
        '-Variables---------------------------------------------------------
        Dim SAPFunc, Connection, SAPConnection, PingFunc, retPing
        Dim exceptPing
    
        '-Get SAP.Functions-------------------------------------------------
        On Error Resume Next
        Set SAPFunc = CreateObject("SAP.Functions.Unicode")
        On Error Goto 0
        If Not IsObject(SAPFunc) Then
          MsgBox "CreateObject(SAP.Functions.Unicode) failed", vbOkOnly, "Error"
          Exit Sub
        End If
    
        '-Get SAP.LogonControl connection-----------------------------------
        On Error Resume Next
        Set Connection = SAPFunc.Connection()
        On Error Goto 0
        If Not IsObject(Connection) Then
          MsgBox "SAPFunc.Connection failed", vbOkOnly, "Error"
          Set SAPFunc = Nothing
          Exit Sub
        End If
    
        '-Set connection parameters-----------------------------------------
        Connection.Client = "001"
        Connection.User = "BCUSER"
        Connection.Password = "minisap"
        Connection.Language = "EN"
        Connection.System = "NSP"
        Connection.HostName = "ABAP"
        Connection.SystemNumber = 0
    
        '-Connect SAP system------------------------------------------------
        SAPConnection = Connection.Logon(0, vbFalse)
        If SAPConnection = 0 Then
          MsgBox "Connection.Logon failed", vbOkOnly, "Error"
          Exit Sub
        End If
    
        '-Call ABAP function module RFC_PING--------------------------------
        Set PingFunc = SAPFunc.Add("RFC_PING")
        If IsObject(PingFunc) Then
          retPing = PingFunc.Call()
          If retPing = False Then
            exceptPing = PingFunc.Exception()
            MsgBox CStr(exceptPing), vbOkOnly, "Result"
          Else
            MsgBox CStr(retPing), vbOkOnly, "Result"
          End If
        End If
    
        '-Logoff------------------------------------------------------------
        Connection.Logoff()
    
      End Sub
    
      '-Main----------------------------------------------------------------
      Main()
    
    '-End-------------------------------------------------------------------
    

    Let us know your results.

    Cheers
    Stefan

    Add comment
    10|10000 characters needed characters exceeded

    • It worked Stefan. I am only getting output now. If you get time to look at below code and assist me. I am already connecting to my SAP system using logon pad which would mean SAPGUI connection is open. But when i run my script i still get a pop-up asking for system connection details. I think it is coming coz of CreateObject("SAP.Function") used in code. I tried GetObject but got syntax error. Can you tell me what can i use here so i dont get another pop-up for system connection as my SAP system is already logged in? Also the script only runs fine when i use FM RFC_PING and errors out for other FM's. Why is that any idea?

      Thank you in advance :)

      On Error Resume Next

      Set SapGuiAuto = GetObject("SAPGUI")

      On Error Goto 0

      If Not IsObject(SapGuiAuto) Then

      MsgBox "SAP logon pad is not running, Click Ok to start"

      Set WshShell = CreateObject("WScript.Shell")

      Set proc = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd \SAPgui\saplogon.exe")

      WScript.Sleep 4000

      Set SapGuiAuto = GetObject("SAPGUI")

      End If

      Set application = SapGuiAuto.GetScriptingEngine

      SID = InputBox("Please enter SID to login")

      If Not IsObject(connection) Then

      Set connection = application.OpenConnection(SID, True) End If

      If Not IsObject(session) Then

      Set session = connection.Children(0) End If

      If IsObject(WScript) Then

      WScript.ConnectObject session, "on"

      WScript.ConnectObject application, "on" End If

      session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "200"

      UID = InputBox("Please enter our ISID")

      session.findById("wnd[0]/usr/txtRSYST-BNAME").text = UID

      Password = InputBox ("Please enter Password")

      session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = Password

      session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus

      session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8

      session.findById("wnd[0]").sendVKey 0

      '**** User Machine Info***************

      Set funcControl = CreateObject("SAP.Functions")

      Set compname = funcControl.Add("TMP_GUI_GET_COMPUTERNAME")

      MsgBox compname (compname gives correct output by showing pop-up with FM name)

      If compname.Call = True Then

      MsgBox "Call successful"

      Else

      MsgBox "Correct the program"

      End If

  • Sep 29, 2017 at 02:47 AM

    Hello Dipesh,

    the SAP GUI and RFC connections are different ways. You have on the one hand a GUI_Connection and on the other hand a RFC_Connection. Take a look at your modified code to use these different kind of connections parallel in one script:

    On Error Resume Next
    Set SapGuiAuto = GetObject("SAPGUI")
    On Error Goto 0
    If Not IsObject(SapGuiAuto) Then
      MsgBox "SAP logon pad is not running, Click Ok to start"
      Set WshShell = CreateObject("WScript.Shell")
      Set proc = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd \SAPgui\saplogon.exe")
      WScript.Sleep 4000
      Set SapGuiAuto = GetObject("SAPGUI")
    End If
    Set application = SapGuiAuto.GetScriptingEngine
    SID = InputBox("Please enter SID to login")
    If Not IsObject(connection) Then
      Set GUI_Connection = application.OpenConnection(SID, True)
    End If
    If Not IsObject(session) Then
      Set session = GUI_connection.Children(0)
    End If
    session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "200"
    UID = InputBox("Please enter our ISID")
    session.findById("wnd[0]/usr/txtRSYST-BNAME").text = UID
    Password = InputBox ("Please enter Password")
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = Password
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
    session.findById("wnd[0]").sendVKey 0
    
    
    '**** User Machine Info***************
    Set funcControl = CreateObject("SAP.Functions")
    Set RFC_Connection = SAPFunc.Connection()
    RFC_Connection.Client = "200"
    RFC_Connection.User = UID
    RFC_Connection.Password = Password
    RFC_Connection.Language = "EN"
    RFC_Connection.System = SID
    RFC_Connection.HostName = "ABAP"
    RFC_Connection.SystemNumber = 0
    SAPConnection = RFC_Connection.Logon(0, vbTrue)
    Set compname = funcControl.Add("TMP_GUI_GET_COMPUTERNAME")
    If compname.Call = True Then
      MsgBox "Call successful"
    Else
      MsgBox "Correct the program"
    End If
    RFC_Connection.LogOff()
    

    Let us know your result.

    Cheers
    Stefan

    Add comment
    10|10000 characters needed characters exceeded

    • Set SessionInfo = session.Info

      MsgBox SessionInfo.SystemName

      Ablove code worked as expected...

      I ran your other code which you mentioned in one of your blogs. I found out that my mistake was to use Set before using the session info. I removed Set and kept the assignment as it was and worked...

      Let me see if outlook works for me with the help of your code my friend, will let you know the results.

      Thanks again...you have been great help. SAP GUI Scripting pdf document does not give much on the properties in terms of how to use them and all...