Skip to Content
0

SAP.Functions --- Active X component error

Sep 27, 2017 at 01:46 PM

344

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Stefan Schnell
Sep 27, 2017 at 07:49 PM
0

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

Show 4 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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

0
Former Member

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?

0

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

0
Former Member
Stefan Schnell

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

0
Stefan Schnell
Sep 29, 2017 at 02:47 AM
0

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

Show 5 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Stefan,

Thanks again for assisting me here. The Second part of code was not working when i was executing the script by right-click and selecting Open in Command Prompt. But when i ran the code using cmd.exe and gave full path for script, it ran successfully.

I get successful call output when i use RFC_PING FM. But if I use any of these FM's TH_LONG_USR_INFO TMP_GUI_GET_COMPUTERNAME TH_USER_INFO, RFC_READ_TABLE it does not work. Is there anything being missed here. I mean working for 1 and not for others.

Also i would like to know if i can send the output from FM in a mail. I found few blogs for sending mail from SAP, i tried to use the code for testing purpose but its not effective till now.

Option Explicit

Dim MyEmail

Set MyEmail = CreateObject(“CDO.Message”)

MyEmail.Subject = “Subject Line”

MyEmail.From = “email address“

MyEmail.To = "email address"

MyEmail.TextBody = “Test message”

MyEmail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/sendusing“)= 2

MyEmail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpserver“)= “mail host server name”

MyEmail.Configuration.Fields.Item (“http://schemas.microsoft.com/cdo/configuration/smtpserverport“)= 25 MyEmail.Configuration.Fields.Update

MyEmail.Send

0

Hello Dipesh,

I assume that you use Windows x64. If you open in this case the context menu of a script file and executes it, the system uses Windows Scripting Host x64. But ActiveX library SAP.Functions is only available with x86, you can't use it on this way. It is necessary to start your script e.g. via console command C:\Windows\SysWOW64\wscript.exe to be ensure that you use also the x86 version.

TH_LONG_USR_INFO is not a remote enabled function module (RFM), also TMP_GUI_GET_COMPUTERNAME and TH_USER_INFO. You can't use this kind of FMs on this way. If you want to use it as RFM you must code a wrapper in ABAP or use RFC_ABAP_INSTALL_AND_RUN. RFC_READ_TABLE should work, but it needs QUERY_TABLE as import parameter, otherwise you get no result.

I assume you want to send an email from your script with the returned values from a FM. I never use Microsofts Collaboration Data Objects (CDO), I use only Outlook, so I can't say what is the correct way to use it.

Cheers
Stefan

0
Former Member
Stefan Schnell

Thanks Stefan for your inputs.

I tried to change FM's to RFM, still not able to execute them using script. Now i got it why RFC_READ_TABLE was not working, will try to pass parameter value and let you know the results.

I used RFC_ABAP_INSTALL_AND_RUN as well, but it asks for program name instead of FM. I ll search for program name which i need and provide me required output.

When i was searching for sending mails through scripts, i came across a blog in which user has used CDO.

You use Outlook, do you mean within script or normal operations? Is there any way you are aware which can be used to send mails directly from scripts?

Also, i read one of your blogs for using GuiSessioninfo where you have used SystemName() in the program. I tried to use that in my program, but got error. I found another one where it was mentioned to use SessionInfo with the property as session.info.user or session.info.transaction. But it also gave me error.

Can you suggest a way, how to utilize GuiSessionInfo properties to display some of the information from session?

0

Hello Dipesh,

you can't change FMs from SAP, you must code wrapper in your customer namespace.

I use Outlook from the script like this:

Set Outlook = CreateObject("Outlook.Application")
Set Mail = objOutlook.CreateItem(0)
Mail.To = "dipesh.jhamb@example.com"
Mail.Subject = "Test Mail"
Mail.Body = "Hello Dipesh"
Mail.Send
Outlook.Quit
Set Mail = Nothing
Set Outlook = Nothing

I never use another way like this.

To use SessionInfo is very easy, add this code to your example above

Set SessionInfo = session.Info
MsgBox SessionInfo.SystemName

and you should see the system name of your SAP system.

Cheers
Stefan

0
Former Member
Stefan Schnell
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...

1