Retreive Displayed text field in SAP through macro with Excel - excel

I tried to run the SAP script recording to gather a text that is displayed for several item numbers automatically. While I was checking it this is the code I got :
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
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]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "CS03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").setFocus
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").caretPosition = 0
session.findById("wnd[0]").sendVKey 4
session.findById("wnd[1]/usr/lbl[1,10]").setFocus
session.findById("wnd[1]/usr/lbl[1,10]").caretPosition = 0
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/ctxtRC29N-MATNR").text = "508546"
session.findById("wnd[0]/usr/ctxtRC29N-WERKS").text = "1000"
session.findById("wnd[0]/usr/ctxtRC29N-WERKS").setFocus
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tblSAPLCSDITCALT/ctxtRC29K-STLST[1,15]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/tblSAPLCSDITCALT/ctxtRC29K-STLST[1,15]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCDO").select
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA").select
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-KTEXT[3,3]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPDAT").select
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-KTEXT[3,6]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPDAT/ssubSUBPAGE:SAPLCSDI:0840/btnRC29P-ICON1").press
session.findById("wnd[0]/usr/cntlSCMSW_CONTAINER_2102/shellcont/shell").setDocument 1,"e1xydGYxXGFkZWZsYW5nMTAyNVxhbnNpXGFuc2ljcGcxMjUyXHVjM="
Obviously, I did not get the required result as I want to retrieve the displayed text and not the text field. Any ideas on how to get that?

First of all instead of .setFocus method try to use .Text method. Then create variable like strMyText. After that assign text from SAP to this variable and at the end paste it somewhere.
So it will be something like this:
Sub SAPText()
Dim strMyText
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
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]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "CS03"
session.findById("wnd[0]").sendVKey 0
strMyText = session.findById("wnd[0]/usr/ctxtRC29N-STLAN").Text
Activecell.value = strMyText
End sub

Related

SAP Script using loop

I need to increment some values from excel file, in SAP.
I already make script for this, but it is work only for 1 value.
I need to pick value from column A and add in value from column B.
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Sub Changelotzise()
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
End If
If objConn Is Nothing Then
Set objConn = objGui.Children(0)
End If
If session Is Nothing Then
Set session = objConn.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject objGui, "on"
End If
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmm02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = value from A1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").Text = value from B1
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").SetFocus
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").caretPosition = 2
session.findById("wnd[0]").sendVKey 11
End Sub
After this I need to pick value from A2 and B2
Can you help me?
Thank you!
I try to use this:
material = ActiveWorkbook.ActiveSheet.Range("A2").Value
lotsize = ActiveWorkbook.ActiveSheet.Range("B2").Value
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmm02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = material
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").Text = lotsize
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").SetFocus
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").caretPosition = 2
session.findById("wnd[0]").sendVKey 11
End Sub
But I need to pick a Range not a single value

Format string based on current date and time

I want to extract a file from SAP, however, I need the name of this file to be dynamic at the time of export, taking the time it was extracted.
For example, if it was extracted at 12:00, I need the name to be "12-00.XLSX"
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
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]").resizeWorkingPane 142,25,false
session.findById("wnd[0]/tbar[0]/okcd").text = "zv15"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_VKORG").text = "f86c"
session.findById("wnd[0]/usr/ctxtP_VKBUR").text = "br01"
session.findById("wnd[0]/usr/ctxtS_MONTH-LOW").text = "1"
session.findById("wnd[0]/usr/ctxtS_MONTH-HIGH").text = "12"
session.findById("wnd[0]/usr/ctxtS_MONTH-HIGH").setFocus
session.findById("wnd[0]/usr/ctxtS_MONTH-HIGH").caretPosition = 2
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[4]/menu[11]").select
session.findById("wnd[1]/usr/ctxtLAST_LOGON_TIME").setFocus
session.findById("wnd[1]/usr/ctxtLAST_LOGON_TIME").caretPosition = 8
session.findById("wnd[1]").close
session.findById("wnd[0]/tbar[1]/btn[43]").press
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "11-50-30.XLSX"
session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 8
session.findById("wnd[1]/tbar[0]/btn[0]").press```
In your example, you used the time from the previous login. I present an example of real system time.
e.G.
...
session.findById("wnd[0]/mbar/menu[4]/menu[11]").select
myTime = session.findById("wnd[1]/usr/ctxtSYST-UZEIT").text
myName = replace(myTime, ":", "-") & ".xlsx"
session.findById("wnd[1]").close
session.findById("wnd[0]/tbar[1]/btn[43]").press
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = myName
...
Regards, ScriptMan

Excel SAP GUI Compile error label not defined

I have this SAP GUI script that I have recorded as seen below. The macro should be able to export it to an excel sheet. However, I need a way to where it will actually populate the excel sheet. However i keep getting this error: label not found. Can someone please help me out ? I saw this question on this right here: How to run SAP GUI script from Excel Macro
However, I followed it almost identically and I'm trying to troubleshoot where I'm messing up.
RAW SAP MACRO RECORDER:
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
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]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "ZPRS"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_PLANT").text = "1707"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").text = "BSLSR"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").setFocus
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").caretPosition = 5
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
looking at the link and changing it up to what I thought was correct
Private Sub CommandButton1_Click()
On Error GoTo Err_NoSAP
If Not IsObject(SAPGuiApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPGuiApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPGuiApp.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject SAPGuiApp, "on"
End If
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = SAP_session.ActiveWindow()
aw.findById("wnd[0]").Maximize
On Error GoTo Err_Description
session.findById("wnd[0]").Maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "ZPRS"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_PLANT").Text = "1707"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").Text = "BSLSR"
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").SetFocus
session.findById("wnd[0]/usr/ctxtS_ARBPL-LOW").caretPosition = 5
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
Exit Sub
End Sub
but then I get error that says :
Compile error: label not defined
Does anyone have any ideas on how to correct this ?
Well, on first look you are using error handlers like On Error GoTo Err_NoSAP. There is nowhere in your posted code line with error handler like Err_NoSAP:, where code should after occured error jump into.

How to vary data using SAP and vba?

First, I was able to connect SAP GUI to vba. I managed to get a script that works and that sends me an automatic transaction. I would like to know how to make the data entered on SAP for example the date is variable because the date I have to change it every year?
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
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/tabsTABSTRIP_TABBL1/
tabpUCOM1/ssub%_SUBSCREEN_TABBL1:RFBILA00:0001/txtBILBJAHR").text = "2015"
For example, I would like the "2015" date to be variable, I would like every month I can change this date to put another one without having to start the extraction on SAP GUI again.
Like this, you should also indent your code:
Option Explicit
Sub Test()
Dim MyYear As String
MyYear = Format(Date, "yyyy")
If Not IsObject(Application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Application.Children(0)
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/tabsTABSTRIP_TABBL1/tabpUCOM1/ssub%_SUBSCREEN_TABBL1:RFBILA00:0001/txtBILBJAHR").Text = MyYear
End Sub

VBA to call SAP transaction

I am trying to call a SAP transaction from Excel, I use many codes from the internet but still a failure.
Doesn't matter what code I use, I always get same error message as
A script is opening a connection to system SYSTEM
than error saying:
Runtime error 1000 Error description not avialable"
Than after debug it highlights below in yellow
Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)
its been doing this with all code I tried to pull this.
Code
Dim sap As Object
Dim conn As Object
Sub T_login()
Set sap = CreateObject("SAP.Functions")
Set conn = sap.Connection
conn.System = "SYSTEM"
conn.client = "603"
conn.user = "LANAX001"
conn.Password = "alamzeb4"
conn.Language = "EN"
If conn.logon(0, False) Then
MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment"
Else
End If
If Not IsObject(SapGuiApp) Then
Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If
If Not IsObject(Connection) Then
Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
'launch a transaction
session.findById("wnd[0]").Maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "FS10N"
session.findById("wnd[0]").sendVKey 0
End Sub
This is what I use to load data to SAP - verified to work. HTH.
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject App, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nXXXX" 'XXX = your t-code
... ...
Afterwards, it depends on what you do in the t-code.
Good luck!

Resources