Search code examples
excelvbaerror-handlingruntime-errorsap-gui

Not able to bypass Run time error 619 in SAP GUI connection


I have a macro to download certain SAP GUI reports to Excel. My issue is that if I run the macro more than once in the same SAP GUI session (first time it works fine), I get this error which I am not able to bypass:

Run-time error 619

For some reason (I think it's related to which server the user is logged on to), the SAP ERP module (RE-FX) have two different variants/GUIs. Therefore, I have two different setups for downloading the report to Excel depending on the variant/GUI.

I am using the On Error Goto statement to shift between those two variants. The Run time error appears in the line following the On Error Goto statement.

As mentioned, this works fine the first time I run the macro (no Run Time error occurs and the macro jumps to the error handler as expected), but the second time I run it, the error '619' appears and it is not possible to bypass it.

I have tried the solution in this post (including Application.Wait): Cannot Bypass Error 619 "Control not found"

But that did not fix it (it is not the timing which is the issue here).

Sub Run_REISCDCF()

 Dim Filepath As String
 Dim ReportDate As String
 Dim SapGuiAuto As Object
 Dim SAPApp As Object
 Dim SAPCon As Object
 Dim session As Object


 Filepath = ThisWorkbook.Sheets("Guide").Cells(5, 5).Text   'place to store SAP reports

'Create connection to SAP
'------------------------------------------
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set session = SAPCon.Children(0)
'------------------------------------------

'Removed some code to run the report and change layout (which works fine)

'Save to Excel
    session.findById("wnd[0]/usr/subSUB_AREA_ROOT:SAPLREIS_GUI_CONTROLLER:0200/subSUB_AREA:SAPLREIS_GUI_CONTROLLER:1000/cntlCC_LIST/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
    session.findById("wnd[0]/usr/subSUB_AREA_ROOT:SAPLREIS_GUI_CONTROLLER:0200/subSUB_AREA:SAPLREIS_GUI_CONTROLLER:1000/cntlCC_LIST/shellcont/shell").selectContextMenuItem "&XXL"
    
    On Error GoTo XLSX_variant 'SAP has two different GUI's for RE-FX with one of them only allowing to download to a MHTML file type
    session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "Filepath" '<-- At this line the Run Time error appears
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "REISCDCF.MHTML"
    session.findById("wnd[1]/tbar[0]/btn[11]").press
Exit Sub

XLSX_variant:
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "Filepath"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "REISCDCF.XLSX"
    session.findById("wnd[1]/tbar[0]/btn[11]").press

Exit Sub

Solution

  • Try this...

    If Not session.findById("wnd[1]/tbar[0]/btn[0]", False) Is Nothing Then
    
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    
    End If
    

    This code will lookup for the button in the session you are and if it finds it it will click on it else it means it’s not there and I’ll continue with next line.

    Sub Run_REISCDCF()
    
     Dim Filepath As String
     Dim ReportDate As String
     Dim SapGuiAuto As Object
     Dim SAPApp As Object
     Dim SAPCon As Object
     Dim session As Object
    
    
     Filepath = ThisWorkbook.Sheets("Guide").Cells(5, 5).Text   'place to store SAP reports
    
    'Create connection to SAP
    '------------------------------------------
    Set SapGuiAuto = GetObject("SAPGUI")
    Set SAPApp = SapGuiAuto.GetScriptingEngine
    Set SAPCon = SAPApp.Children(0)
    Set session = SAPCon.Children(0)
    '------------------------------------------
    
    'Removed some code to run the report and change layout (which works fine)
    
    'Save to Excel
        session.findById("wnd[0]/usr/subSUB_AREA_ROOT:SAPLREIS_GUI_CONTROLLER:0200/subSUB_AREA:SAPLREIS_GUI_CONTROLLER:1000/cntlCC_LIST/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
        session.findById("wnd[0]/usr/subSUB_AREA_ROOT:SAPLREIS_GUI_CONTROLLER:0200/subSUB_AREA:SAPLREIS_GUI_CONTROLLER:1000/cntlCC_LIST/shellcont/shell").selectContextMenuItem "&XXL"
    
     'SAP has two different GUI's for RE-FX with one of them only allowing to download to a MHTML file type
    
    If Not session.findById("wnd[1]/tbar[0]/btn[0]", False) Is Nothing Then
    
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    
    End If
    
        session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "Filepath" '<-- At this line the Run Time error appears
        session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "REISCDCF.MHTML"
        session.findById("wnd[1]/tbar[0]/btn[11]").press
    
    Exit Sub