cancel
Showing results for 
Search instead for 
Did you mean: 

SAP.Functions --- Active X component error

Former Member
0 Kudos

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

View Entire Topic
stefan_schnell
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Stefan,

I am connecting to SAP system first and then only trying to invoke this. But it only gives output from ELSE statement in an If..Else loop. I wrote below code to confirm if the call is getting through to FM or not.

Please refer to below ( I used your suggestion from one of my other posts to open SAPGUI)

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") (I get Active X error when i double click on my script, however it works fine when i run using cmd.exe. Any option i can run by double clicking the script itself)

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 (It fails to get into If loop and always shows Else statement)

MsgBox "Call successful"

Else

MsgBox "Correct the program"

End If

Former Member
0 Kudos

Hi Stefan,

Just an update, if i run your code with RFC_PING it works like a charm.

When i use any other FM it doesn't like TMP_GUI_GET_COMPUTERNAME.

Also i made few changes to my code (using yours ofcourse) and it works fine for RFC_PING.

But even with SAP screen open on my end, there comes a pop-up for system details. I think it comes coz of createobject(SAP.Functions), how to remove that pop-up?

stefan_schnell
Active Contributor
0 Kudos

Hello Dipesh,

change the line

SAPConnection = Connection.Logon(0, vbFalse)

to

SAPConnection = Connection.Logon(0, vbTrue)

Now you should don't see the logon screen.

Cheers
Stefan

Former Member
0 Kudos

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