Error in execution of SAP Scritping with VBA - excel

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

Related

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.

SAP GUI Scripting in Excel VBA - Error 91 (Object Variable...)

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

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

Using SAPGUI in excel/VBA, Why does changing the .VerticalScrollbar.Position of a GuiTableControl breaks the object "connection"?

First my test code
Sub mytest2()
If Not IsObject(MyApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set MyApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = MyApplication.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 MyApplication, "on"
End If
Dim MyTableStr As String
Dim MyTable As Variant
MyTableStr = "wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpVGUE/ssubSUB_AUFTRAG:SAPLCOVG:3010/tblSAPLCOVGTCTRL_3010"
Set MyTable = session.findById(MyTableStr)
MyTable.VerticalScrollbar.Position = MyTable.VerticalScrollbar.Position + 1
Debug.Print MyTable.VerticalScrollbar.Position & " position"
This code will increase the table's vertical position and then fail on the debug.print line with error "Object doesn't support this action" Run-time error 445
If I add again the line
Set MyTable = session.findById(MyTableStr)
Between the .position line and the debug.print line, it works and the position gets printed.
Why does this happen ?
I found the answer in the SAP GUI scripting API pdf at
http://www.synactive.com/download/sap%20gui%20scripting/sap%20gui%20scripting%20api.pdf
The important passage was in the description of the GuiButton control.
Function press
This emulates manually pressing a button. Pressing a button will always cause server communication to occur, rendering all references to elements below the window level invalid. The following code will therefore fail:
Set TextField = session.findById(".../txtF1")
session.findById(".../btnPB5").press
TextField.text = "Hello"
This is because when you execute commands, some will only affect the client state but other commands will cause communication with server and apparently when that happens, all previous object references are broken.
Changing the .VerticalScrollbar.Position of a GuiTableControl also causes communication with the server.

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