I'm trying to run a SAP GUI script using a button in Excel, I have the script created in a way that it takes info from Excel to use in SAP, that script works great, if I open it, it runs smoothly but I can't manage to add that script to Excel command button.
This is the original script that works:
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
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").text = Col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).selected = true
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").setFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").setFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").selected = true
session.findById("wnd[0]/usr/ctxtNAST-LDEST").text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").text = Col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").setFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
aux=col1 & " " & col2
CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
next
msgbox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
If I add this code to Excel VBA it doesn't work, so I followed this post and I changed 'application' to 'GuiApplication' and did a few changes based on the code that was given on that post answer.
After doing the changes I ended up with this code:
Private Sub CommandButton1_Click()
If Not IsObject(GuiApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set GuiApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = GuiApplication.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 GuiApplication, "on"
End If
If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP
Set aw = session.ActiveWindow()
aw.findById("wnd[0]").Maximize
'REM ADDED BY EXCEL *************************************
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 To objSheet.UsedRange.Rows.Count
col1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
col2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
'REM ADDED BY EXCEL *************************************
On Error GoTo Err_Description
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00028"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00028"
session.findById("wnd[0]/usr/ctxtVBRK-VBELN").Text = col1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[3]").Select
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3").getAbsoluteRow(0).Selected = True
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/lblDV70A-STATUSICON[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"
session.findById("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").SetFocus
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/chkNAST-DELET").Selected = True
session.findById("wnd[0]/usr/ctxtNAST-LDEST").Text = "LOCALNEW"
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").Text = col2
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").SetFocus
session.findById("wnd[0]/usr/txtNAST-TDRECEIVER").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
'REM FINALIZATION CONTROL CHECK ************************
aux = col1 & " " & col2
CreateObject("WScript.Shell").Run ("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
Next
MsgBox "Process Completed"
'REM FINALIZATION CONTROL CHECK ************************
End Sub
When I hit Debug > Compile VBA project, an error pops saying
Compile error: Label not defined
and highlights a line on the code Set objExcel = GetObject(, "Excel.Application")

Any ideas of what I'm doing wrong? I'm sure I'm doing a lot of things wrong, I'm learning. I just need to be able to run that script with a click of a button on Excel.