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

Sis*_*iso 2 error-handling excel vba runtime-error sap-gui

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
Run Code Online (Sandbox Code Playgroud)

小智 5

尝试这个...

\n\n
If Not session.findById("wnd[1]/tbar[0]/btn[0]", False) Is Nothing Then\n\nsession.findById("wnd[1]/tbar[0]/btn[0]").press\n\nEnd If\n
Run Code Online (Sandbox Code Playgroud)\n\n

此代码将在您所在的会话中查找按钮,如果找到它,它将单击它,否则意味着它 \xe2\x80\x99 不在那里,而 I\xe2\x80\x99 将继续下一行。

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