Skip to Content
avatar image
Former Member

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jan 09, 2015 at 08:21 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • Feb 04, 2015 at 12:54 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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