VBA Code to extract SAP Report - support required to make the code to wait after execution

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
 

Attachments

  • code.png
    code.png
    82.1 KB · Views: 65

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Do you get an error on the next line if you just let it run?
 
Upvote 0
Maybe you could try something like the below, you could also add a sleep in the loop if needed:
VBA Code:
Do
    On Error Resume Next
    Session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select 'Select menu bar to save as
    If Err.Number = 0 Then Exit Do
    DoEvents
Loop
On Error GoTo 0
 
Upvote 0
Maybe you could try something like the below, you could also add a sleep in the loop if needed:
VBA Code:
Do
    On Error Resume Next
    Session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select 'Select menu bar to save as
    If Err.Number = 0 Then Exit Do
    DoEvents
Loop
On Error GoTo 0
Still i'm getting the same error - approx. after 1 Min.

Microsoft excel is waiting for 'system name' to complete an OLE action
 
Upvote 0
With my addition, what line do you get your error on?
 
Upvote 0
With my addition, what line do you get your error on?
Not exactly the error but i'm getting the dailogbox in excel asking to Click "ok" - I have to keep clicking the Ok button....and if the report takes mre time this dailogbox appears more than 40 to 50 times...where I have to keep clicking "OK" button....

Microsoft excel is waiting for 'system name' to complete an OLE action
 
Upvote 0
If the code completes after clicking ok (a lot) then maybe the below will help?
VBA Code:
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)
    
    Application.DisplayAlerts = False
    
    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
        With Session
            .findById("wnd[0]/usr/txtSCOUNTRY-LOW").Text = country 'Input Country
            .findById("wnd[0]/usr/txtSCOUNTRY-LOW").SetFocus
            .findById("wnd[0]/usr/txtSCOUNTRY-LOW").caretPosition = 2
            .findById("wnd[0]").sendVKey 8 'Execute button ----------------------> Help requried after this step - code to wait for the next element to be avaiable..
            Do
                On Error Resume Next
                Session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select 'Select menu bar to save as
                If Err.Number = 0 Then Exit Do
                DoEvents
            Loop
            On Error GoTo 0
            .findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
            .findById("wnd[1]/tbar[0]/btn[0]").press
            .findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\SAP Demo Exports" 'Input save as directory
            .findById("wnd[1]/usr/ctxtDY_FILENAME").Text = country & " Customers.xlsx" 'Input file name
            .findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 17
            .findById("wnd[1]/tbar[0]/btn[0]").press 'Confirm and OK
            .findById("wnd[0]/tbar[0]/btn[3]").press 'Back button
        End With
        ThisWorkbook.Sheets("Sheet1").Range("B" & i).Value = "C:\SAP Demo Exports\" & country & " Customers.xlsx"
    Next i
    
    Application.DisplayAlerts = True
    
    ThisWorkbook.Save
    Application.Quit
    ThisWorkbook.Saved = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top