Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Loop does not work on VBA Script

Hi Guys,

I'm having troubles when I try to execute the script below. The first input works fine, but the loop does not.

Somebody has a clue what is wrong on the code listed below?

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

Dim ApplicationPath, CredentialsPath, ServerPath, FilePath
Dim ExcelApp, ExcelWorkbook, ExcelSheet, Row
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelWorkbook = ExcelApp.Workbooks.Open("C:\temp\Excel_File.xls")
Set ExcelSheet = ExcelWorkbook.Worksheets(1)

Row = 1

Do Until ExcelSheet.Cells(Row,1).Value = ""

session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "mysaplogin"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "*******"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[0]/tbar[0]/okcd").text = "fd32"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/chkRF02L-D0210").selected = true
session.findById("wnd[0]/usr/ctxtRF02L-KUNNR").text = ExcelSheet.Cells(Row,1).Value
session.findById("wnd[0]/usr/ctxtRF02L-KKBER").text = ExcelSheet.Cells(Row,2).Value
session.findById("wnd[0]/usr/ctxtRF02L-KKBER").setFocus
session.findById("wnd[0]/usr/ctxtRF02L-KKBER").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[0]/menu[5]/menu[1]").select
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0

Row=Row+1
Loop
ExcelApp.Quit
Set ExcelApp = Nothing
Set ExcelWorkbook = Nothing
Set ExcelSheet = Nothing

Former Member
Not what you were looking for? Ask a question