cancel
Showing results for 
Search instead for 
Did you mean: 

Loop does not work on VBA Script

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

thomas_brutigam2
Active Participant
0 Kudos

Hi

I think it should be like this:

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

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


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

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
0 Kudos

Hi Thomas,

On the excel file I have:

The script still not taking the loop. Always repeating the row 1.

Thanks,

Douglas

thomas_brutigam2
Active Participant
0 Kudos

Hi Douglas,

Please try the Following:


Row = 1
Do

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 Until ExcelSheet.Cells(Row,1).Value = ""

if this still not works pls reply

script_man
Active Contributor
0 Kudos

Hi Douglas,

you could try the following:

. . .

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

Do Until ExcelSheet.Cells(Row,1).Value = ""
session.findById("wnd[0]/tbar[0]/okcd").text = "fd32"
session.findById("wnd[0]").sendVKey 0

. . .

Regards,

ScriptMan

Former Member
0 Kudos

Script Man, I'm sorry for the later reply. Anyway I really appreciate your quickly answer.

It keeps not take the loop repeating the first raw.

Thank you for your time.

script_man
Active Contributor
0 Kudos

Hi Douglas,

Unfortunately, I have not access to the transaction fd32. But you could try the following:

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

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


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

session.findById("wnd[0]/tbar[0]/okcd").text = "/nfd32"

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

'Let the new values show as test.

'msgbox ExcelSheet.Cells(Row,1).Value

'msgbox ExcelSheet.Cells(Row,2).Value


Loop
ExcelApp.Quit
Set ExcelApp = Nothing
Set ExcelWorkbook = Nothing
Set ExcelSheet = Nothing

Regards,

ScriptMan