Skip to Content
0

SAP GUI-Scripting (RFC Login)

Mar 30, 2017 at 10:56 AM

471

avatar image
Former Member

hi friend,

i had problem to connect sap to create session (back-ground),

this below code work perfect when sap gui manually login. but it's doesn't work in RFC login.

   Set SAPGuiAuto = GetObject("SAPGUI")
   Set Application = SAPGuiAuto.GetScriptingEngine
   Set Connection = Application.Children(0)
   Set session = Connection.Children(0)

session.FindById("wnd[0]/tbar[0]/okcd").Text = "/n fi01"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/usr/ctxtBNKA-BANKS").Text = Range("D11").Value
session.FindById("wnd[0]/usr/ctxtBNKA-BANKL").Text = Range("F11").Value
session.FindById("wnd[0]/usr/ctxtBNKA-BANKL").SetFocus
session.FindById("wnd[0]/usr/ctxtBNKA-BANKL").CaretPosition = 7
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/usr/txtBNKA-BANKA").Text = Range("H11").Value
session.FindById("wnd[0]/usr/ctxtBNKA-PROVZ").Text = Range("J11").Value
session.FindById("wnd[0]/usr/txtBNKA-STRAS").Text = Range("L11").Value
session.FindById("wnd[0]/usr/txtBNKA-ORT01").Text = Range("N11").Value
session.FindById("wnd[0]/usr/txtBNKA-BRNCH").Text = Range("D13").Value
session.FindById("wnd[0]/usr/txtBNKA-SWIFT").Text = Range("F13").Value
session.FindById("wnd[0]/usr/txtBNKA-BNKLZ").Text = Range("H13").Value
session.FindById("wnd[0]/usr/txtBNKA-BNKLZ").SetFocus
session.FindById("wnd[0]/usr/txtBNKA-BNKLZ").CaretPosition = 7
session.FindById("wnd[0]/tbar[0]/btn[11]").Press

I'm using below code to connect my system to sap via RFC

Set sap = CreateObject("SAP.Functions.unicode")
Set conn = sap.Connection
conn.system = Range("D4").Value
conn.Client = Range("F4").Value
conn.user = Range("H4").Value
conn.Password = "Abap@Yash"
conn.Language = Range("L4").Value
If conn.logon(0, False) Then
login = True
MsgBox "Login sucessfull."

The above both code's are work separately ,i want to merge above both code .

is it possible to merge ?

if any way is there please guide to solve this problem .

Thanks ,

Umayaraj

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

2 Answers

Stefan Schnell
Mar 31, 2017 at 10:56 AM
1

Hello Umayaraj,

"SAP.Functions.unicode" is a wrapper around the classic RFC libraries. In a normal case this kind of connection works without an UI. As far as I know it isn't possible to combine the RFC library and SAP GUI Scripting on this way. Here an example:

Sub Test()

  Dim sap As SAPFunctionsOCX.SAPFunctions
  Dim conn As SAPLogonCtrl.Connection
  Dim TA As SAPFunctionsOCX.Function

  Set sap = CreateObject("SAP.Functions.unicode")
  Set conn = sap.Connection
  conn.system = "NSP"
  conn.Client = "001"
  conn.user = "BCUSER"
  conn.Password = "minisap"
  conn.Language = "en"
  'Set this parameter to allow RFC to work with an UI
  conn.RfcWithDialog = True
  If conn.Logon(0, True) Then
    'Here I call any transaction code, in my case SE16
    Set TA = sap.Add("RFC_CALL_TRANSACTION_USING")
    TA.Exports("TCODE") = "SE16"
    TA.Exports("MODE") = "Y"
    TA.Call    

    'Your Script here doesn't work, because the call of the RFC is synchronous
    'This code was executed if the transaction is terminated
    Set SAPGuiAuto = GetObject("SAPGUI")
    Set App = SAPGuiAuto.GetScriptingEngine
    Set Connection = App.Children(0)
    Set session = Connection.Children(0)
    
    session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").Text = "TADIR"
    session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").caretPosition = 5
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/tbar[1]/btn[8]").press
    
    conn.Logoff
  End If

End Sub

Please take a look here to find an alternative to open the SAP Logon.

Let us know your results.

Cheers
Stefan

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Apr 01, 2017 at 09:59 AM
0

Hi stefan ,

Thanks for help full guide,now i my program work's perfect ,but problem is it's login SAP GUI ,Process screen & close function is visible ,my current code is updated below is there any way to hide GUI LOGIN & Close (it's mean hidden background process)

  Dim SapGui
  Dim Applic
  Dim connection
  Dim session
  Dim WSHShell
  Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
  Set WSHShell = CreateObject("WScript.Shell")
  Do Until WSHShell.AppActivate("SAP Logon ")
  Loop
  Set WSHShell = Nothing
  Set SapGui = GetObject("SAPGUI")
  Set Applic = SapGui.GetScriptingEngine
  Set connection = Applic.OpenConnection("SANDBOX", True)
  Set session = connection.Children(0)
    session.FindById("wnd[0]").Maximize
    session.FindById("wnd[0]/usr/txtRSYST-MANDT").Text = "500"
    session.FindById("wnd[0]/usr/txtRSYST-BNAME").Text = "ABAP"
    session.FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = "Abap@Yash"
    session.FindById("wnd[0]").SendVKey 0
    session.FindById("wnd[0]/tbar[0]/okcd").Text = "/n fi01"
    session.FindById("wnd[0]").SendVKey 0
    session.FindById("wnd[0]/usr/ctxtBNKA-BANKS").Text = Range("D11").Value
    session.FindById("wnd[0]/usr/ctxtBNKA-BANKL").Text = Range("F11").Value
    session.FindById("wnd[0]/usr/ctxtBNKA-BANKL").SetFocus
    session.FindById("wnd[0]/usr/ctxtBNKA-BANKL").CaretPosition = 7
    session.FindById("wnd[0]").SendVKey 0
    session.FindById("wnd[0]/usr/txtBNKA-BANKA").Text = Range("H11").Value
    session.FindById("wnd[0]/usr/ctxtBNKA-PROVZ").Text = Range("J11").Value
    session.FindById("wnd[0]/usr/txtBNKA-STRAS").Text = Range("L11").Value
    session.FindById("wnd[0]/usr/txtBNKA-ORT01").Text = Range("N11").Value
    session.FindById("wnd[0]/usr/txtBNKA-BRNCH").Text = Range("D13").Value
    session.FindById("wnd[0]/usr/txtBNKA-SWIFT").Text = Range("F13").Value
    session.FindById("wnd[0]/usr/txtBNKA-BNKLZ").Text = Range("H13").Value
    session.FindById("wnd[0]/usr/txtBNKA-BNKLZ").SetFocus
    session.FindById("wnd[0]/usr/txtBNKA-BNKLZ").CaretPosition = 7
    session.FindById("wnd[0]/tbar[0]/btn[11]").Press

  Set session = Nothing
  connection.CloseSession ("ses[0]")
  Set connection = Nothing
  Set sap = Nothing
  MsgBox "Done"

Regards,

Umayaraj

Share
10 |10000 characters needed characters left characters exceeded