Rohith1324
Board Regular
- Joined
- Feb 27, 2018
- Messages
- 114
Hi, Need support on the attached code.
After executing the report if the report screen shows up within few seconds then I dont have any issues with the code but my report doesn't come up within few seconds everytime....based on the Country and the records that are in the SAP system even it takes some time roughly even 5 Min or more than that(but I dont want even the 5 mins to be hardcoded as it changes every now and then)....So I want your support in making the code wait until the report screen to show up....(in the screenshot after the highlighted part)
Option Explicit
Public SapGuiAuto
Public objGui As GuiApplication
Public objConn As GuiConnection
Public Session As GuiSession
Sub SAPExtractCustomerList()
Dim country As String
Dim i As Integer
Dim lastRow As Long
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set Session = objConn.Children(0)
lastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nzcustomer" 'Input T-code
Session.findById("wnd[0]").sendVKey 0 'Enter key
For i = 2 To lastRow
country = ThisWorkbook.Sheets("Sheet1").Range("A" & i).Value
Session.findById("wnd[0]/usr/txtSCOUNTRY-LOW").Text = country 'Input Country
Session.findById("wnd[0]/usr/txtSCOUNTRY-LOW").SetFocus
Session.findById("wnd[0]/usr/txtSCOUNTRY-LOW").caretPosition = 2
Session.findById("wnd[0]").sendVKey 8 'Execute button ----------------------> Help requried after this step - code to wait for the next element to be avaiable..
Session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select 'Select menu bar to save as
Session.findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
Session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\SAP Demo Exports" 'Input save as directory
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = country & " Customers.xlsx" 'Input file name
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 17
Session.findById("wnd[1]/tbar[0]/btn[0]").press 'Confirm and OK
Session.findById("wnd[0]/tbar[0]/btn[3]").press 'Back button
ThisWorkbook.Sheets("Sheet1").Range("B" & i).Value = "C:\SAP Demo Exports\" & country & " Customers.xlsx"
Next i
ThisWorkbook.Save
Application.Quit
ThisWorkbook.Saved = True
End Sub
After executing the report if the report screen shows up within few seconds then I dont have any issues with the code but my report doesn't come up within few seconds everytime....based on the Country and the records that are in the SAP system even it takes some time roughly even 5 Min or more than that(but I dont want even the 5 mins to be hardcoded as it changes every now and then)....So I want your support in making the code wait until the report screen to show up....(in the screenshot after the highlighted part)
Option Explicit
Public SapGuiAuto
Public objGui As GuiApplication
Public objConn As GuiConnection
Public Session As GuiSession
Sub SAPExtractCustomerList()
Dim country As String
Dim i As Integer
Dim lastRow As Long
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set Session = objConn.Children(0)
lastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nzcustomer" 'Input T-code
Session.findById("wnd[0]").sendVKey 0 'Enter key
For i = 2 To lastRow
country = ThisWorkbook.Sheets("Sheet1").Range("A" & i).Value
Session.findById("wnd[0]/usr/txtSCOUNTRY-LOW").Text = country 'Input Country
Session.findById("wnd[0]/usr/txtSCOUNTRY-LOW").SetFocus
Session.findById("wnd[0]/usr/txtSCOUNTRY-LOW").caretPosition = 2
Session.findById("wnd[0]").sendVKey 8 'Execute button ----------------------> Help requried after this step - code to wait for the next element to be avaiable..
Session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select 'Select menu bar to save as
Session.findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
Session.findById("wnd[1]/tbar[0]/btn[0]").press
Session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\SAP Demo Exports" 'Input save as directory
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = country & " Customers.xlsx" 'Input file name
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 17
Session.findById("wnd[1]/tbar[0]/btn[0]").press 'Confirm and OK
Session.findById("wnd[0]/tbar[0]/btn[3]").press 'Back button
ThisWorkbook.Sheets("Sheet1").Range("B" & i).Value = "C:\SAP Demo Exports\" & country & " Customers.xlsx"
Next i
ThisWorkbook.Save
Application.Quit
ThisWorkbook.Saved = True
End Sub