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!
Related
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.
I'm trying to connect to SAP through Excel VBA to run a recorded macro.
When it reaches the start of actual SAP code it places the
Run-Time Error '91'
on the line session.FindById("wnd[0]").Maximize.
If I delete that line it has the same issues with every session line.
I verified the references and declarations.
I ran it as a VBS script and it worked.
I'm logged into a SAP session before running the code.
appl contains Nothing in the value of the Local Windows. On the Type it contains GuiApplication.
Here is the snip of the code:
Private Sub CommandButton1_Click()
Dim Tablename As String
If TextBox1 = "" Then
If Not IsObject(appl) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = appl.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 = "Stock01"
session.FindById("wnd[0]/tbar[0]/btn[0]").press
session.FindById("wnd[0]/usr/ctxtS_MATNR-LOW").Text = "566666"
session.FindById("wnd[0]/usr/ctxtS_MATNR-HIGH").Text = "5666666"
session.FindById("wnd[0]/usr/ctxtS_WERKS-LOW").Text = "1111"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").Text = "1045"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").SetFocus
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").caretPosition = 4
session.FindById("wnd[0]/tbar[1]/btn[8]").press
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").selectContextMenuItem "&XXL"
session.FindById("wnd[1]/tbar[0]/btn[0]").press
session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Documents\SAP_GUI_Code"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Stock.XLSX"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
session.FindById("wnd[1]/tbar[0]/btn[11]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
exit Sub
You say that appl contains Nothing. So it's normal that session also contains Nothing, hence the error 91 when the variable is used.
As #GSerg said in the conversation:
That would be because IsObject determines if the variable has an object type, not whether it contains an instance of an object. In particular, IsObject(Nothing) is True. You want to replace all your IsObject calls with Is Nothing tests.
Consequently, the first lines should be:
If appl Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Connection Is Nothing Then
Set Connection = appl.Children(0)
End If
If session Is Nothing Then
Set session = Connection.Children(0)
End If
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
i'm trying to execute a SAP script with vba but it is showing the following error
"The enumeration of the collection cannot find en element with the specified index"
I've tried some sollutions, but nothing has worked so far?
The button to record script in sap, went completely blank, out of nowhere.
I use the code below, you just need to change the OpenConnection command to the "SAP Program" that i'll use, in this case i'm using the R/3 PCL nuevo
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbHide
Set WSHShell = CreateObject("WScript.Shell")
Do Until WSHShell.AppActivate("SAP Logon ")
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay > 1 'tempo pra abrir o sap
Loop
Set WSHShell = Nothing
If Not IsObject(appl) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = appl.OpenConnection("R/3 PCL nuevo", 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 appl, "on"
End If
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