cancel
Showing results for 
Search instead for 
Did you mean: 

Export SAP scripting field value into VBA

nguyen_huynh
Explorer
0 Kudos

Hello, I am lookin for help.

I have a script which goes to SU01 and select the filed CLASS in user master record. I would like to export the field value of field CLASS into my VBA where I start the SAP scripting. Enclosed my code which does not work.. Thanks for help. Nguyen

Sub test()


If Not IsObject(SAPguiApp) Then
Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If
If Not IsObject(Connection) Then
Set Connection = SAPguiApp.OpenConnection("xxx", True)

End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "xxx"

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

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

session.findById("wnd[0]").resizeWorkingPane 120, 37, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "SU01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtSUID_ST_BNAME-BNAME").Text = "xxx"

session.findById("wnd[0]/tbar[1]/btn[7]").press
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO").Select
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/ctxtSUID_ST_NODE_LOGONDATA-CLASS").SetFocus
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/ctxtSUID_ST_NODE_LOGONDATA-CLASS").caretPosition = 5


session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/ctxtSUID_ST_NODE_LOGONDATA-CLASS").Text = n   '--------->error, does not work, what is the correct code?


session.findById("wnd[0]/tbar[0]/btn[15]").press
session.findById("wnd[0]/tbar[0]/btn[15]").press

MsgBox n


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


End Sub

Accepted Solutions (1)

Accepted Solutions (1)

script_man
Active Contributor
0 Kudos

Hi Nguyen Huynh,

I would try this:

. . .

n = session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/ctxtSUID_ST_NODE_LOGONDATA-CLASS").Text

. . .

Regards,

ScriptMan

nguyen_huynh
Explorer
0 Kudos

Hi ScriptMan,

thanks, this works fine! Respect and have a nice days. Regards Nguyen

Former Member
0 Kudos

Hi Script man,

I hope you're fine and Thanks for your last clarification for my quest.

Now I need a help from you. I don't know how to open a new post and also i tried to send a direct message to you. But I couldn't. Thats why I place this query here. Please help.

I 'm using the follwing (your) code to connect Excel & SAP and have created many automations. All of them are working very fine except one.

The SAP window closes after macro run is completed.

Could you please provide me a code which will not close SAP window after macro run is finished.

And also please provide me a code to transfer data from Excel to a opened (manually) SAP window.

Your timely reply will help me more to work out.

Thanks ScriptMan.

Code:

Set SAP = CreateObject("SAP.Functions")

                  
          Set conn = SAP.connection

       

If Not IsObject(SapGuiApp) Then

           Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1")
          
End If


If Not IsObject(connection) Then

          Set connection = SapGuiApp.OpenConnection("051 - PRD - EP1", True)
End If


If Not IsObject(session) Then

         Set session = connection.Children(0)

End If

UserName = InputBox("Please Enter Your User Name")
Password = InputBox("Please Enter password")

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

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

former_member213011
Participant
0 Kudos

have you tried putting a msgbox to 'pause' the script at the end of your procedure?

e.g.

msgbox "SAP will terminate after you press OK"

Former Member
0 Kudos

Very Thanks for your reply Azmi.

Yes. I already have tried that. But it didn't stop the script. Instead, after we press "ok" it continued with the code and ended the session.

former_member213011
Participant
0 Kudos

Hi Mohanram,

Try declaring the SAPGuiApp as a Public variables (on top of the VBA module) instead of a Private variable within that sub procedure. This way, the SAPGuiApp object will not be reset to 'Nothing' when the procedure ended, hence, not ending the  SAP session.

Another way, I prefer using the set SAPauto = getObject("sapgui") and set SAPGuiApp = SAPAuto.GetScriptingEngine method. This way, the script will bind to a running sapgui application instead of running as an object within the script (using the CreateObject("SAP.Function") method.

Thanks,

MSA

Former Member
0 Kudos

Thank you very much Azmi !!!!

I tried the second one and its working !! Script is not closing the SAP session after macro run.

Is it possible for a script to read and enter into a "logged in" SAP session ?

As of now the script is opening a new session and asks for username, password and then enter into that.

Please advise as this will be more helpful for me.

Thanks

Ram

former_member213011
Participant
0 Kudos

Hi Ram.

Yes you can. You may want to refer to another discussion between myself and Stefan here: http://scn.sap.com/message/13966664#13966664

Thanks,

MSA

Former Member
0 Kudos

Very thanks Azmi. I took code from the above conversation and modified it according to the environment which i have here. Its working very fine. We're able to save plenty of time due to  the automation that i'm doing. All the credits to you only Azmi !!!! Thanks again for your great help on this !!!!!!

Answers (0)