excel 无法绕过SAP GUI连接中的运行时错误619

rbl8hiat  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(130)

我有一个宏来下载某些SAP GUI报告到Excel。我的问题是,如果我在同一个SAP GUI会话中多次运行宏(第一次运行正常),我会得到这个错误,我无法绕过:
运行时错误619
由于某些原因(我认为这与用户登录的服务器有关),SAP ERP模块(RE-FX)有两个不同的变体/GUI。因此,我有两个不同的设置,用于将报告下载到Excel,具体取决于变体/GUI。
我使用On Error Goto语句在这两个变量之间转换。运行时错误出现在On Error Goto语句后面的行中。
如上所述,这在我第一次运行宏时工作正常(没有运行时错误发生,宏按预期跳转到错误处理程序),但第二次运行时,错误'619'出现,无法绕过它。
我在这篇文章中尝试了解决方案(包括Application.Wait):Cannot Bypass Error 619 "Control not found"
但这并没有解决问题(这里的问题不在于时机)。

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
7lrncoxx

7lrncoxx1#

试试这个...

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

这段代码将在会话中查找按钮,如果找到了,它将点击它,否则它意味着它不在那里,我将继续下一行。

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
r6vfmomb

r6vfmomb2#

感谢@reFractil提出了一个有效的解决方案!我不得不稍微修改他的解决方案,以便嵌入两个变体(下载SAP报告为.XLSX或.MHTML),但reFractil提出的结构和代码是相同的:

'Changed code only below

'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"

'Solution:    

If Not session.findById("wnd[1]/tbar[0]/btn[0]", False) Is Nothing Then 
    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" 'Download as .XLSX if in "XLSX SAP_variant"
    session.findById("wnd[1]/tbar[0]/btn[11]").press
    Exit Sub

End If

    session.findById("wnd[1]/usr/ctxtDY_PATH").Text = Filepath
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "REISCDCF.MHTML" 'Download as .MHTML if in "MHTML SAP_variant"
    session.findById("wnd[1]/tbar[0]/btn[11]").press
Exit Sub

相关问题