First of all, I am a newbie using SAP scripting; however, I have used macros in Excel on a regular basis. I have recorded a script in SAP that I want to use as a VBA macro in Excel 2010 that is attached to a button. I have researched several scripts in this discussion board, but none seem to work. Below is the script that I recorded in SAP. The only change I made was to the "application" lines (from "application" to "SAPapplication") since Excel has its own term for "application". I get the error message " the enumerator of the collection cannot find en element with the specified index" at the "Set Connection = SAPApplication.Children(0)" line.
My goal is to click a button on the Excel worksheet, have the user be prompted to select the SAP system to connect to and enter their sign-on information, then have the script run that generates an invoice. Can you please help me?
Sub Button63_Click()
If Not IsObject(SAPApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPApplication.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 SAPApplication, "on"
End If
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "010"
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = " "
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = " "
session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"
session.findById("wnd[0]/usr/txtRSYST-LANGU").SetFocus
session.findById("wnd[0]/usr/txtRSYST-LANGU").caretPosition = 2
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000033"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").topNode = "Favo"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000103"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").topNode = "Favo"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000117"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").topNode = "Favo"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000224"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000229"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "0000000230"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").topNode = "Favo"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "0000000230"
session.findById("wnd[0]/usr/ctxtRF022-BUKRS").Text = "554"
session.findById("wnd[0]/usr/ctxtRF022-BUKRS").caretPosition = 3
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/usr/lbl[1,7]").SetFocus
session.findById("wnd[1]/usr/lbl[1,7]").caretPosition = 3
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[1]/usr/txtRF022-BELNR").Text = "1300000258"
session.findById("wnd[1]/usr/txtRF022-GJAHR").Text = "2013"
session.findById("wnd[1]/usr/txtRF022-GJAHR").SetFocus
session.findById("wnd[1]/usr/txtRF022-GJAHR").caretPosition = 4
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "0000000231"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "0000000231"
session.findById("wnd[0]/usr/ctxtBUKRS-LOW").Text = "554"
session.findById("wnd[0]/usr/txtBELNR-LOW").Text = "1300000258"
session.findById("wnd[0]/usr/txtBELNR-LOW").SetFocus
session.findById("wnd[0]/usr/txtBELNR-LOW").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/lbl[1,11]").SetFocus
session.findById("wnd[0]/usr/lbl[1,11]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[0]/btn[86]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press
End Sub