我有一个宏可以将某些SAP报告下载到Excel。我的问题是,如果我在同一SAP会话中多次运行宏(第一次运行良好),则会收到运行时错误619,无法绕过该错误。
由于某种原因(我认为这与用户登录到的服务器有关),SAP模块(RE-FX)具有两个不同的变体/ GUI。因此,根据变体/ GUI,我有两种不同的设置可用于将报告下载到Excel。
我正在使用On Error Goto语句在这两个变体之间切换。运行时错误出现在On Error Goto语句之后的行中。
如上所述,这在我第一次运行宏时运行正常(没有运行时错误发生,并且宏按预期方式跳转到错误处理程序),但是第二次我运行它时,出现错误“ 619”并且它无法绕过它。
我已经尝试了本文中的解决方案(包括Application.Wait): Cannot Bypass Error 619 [executing SAP from VBA]
但这并不能解决问题(这不是时间问题)。
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
答案 0 :(得分:2)
尝试一下...
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
答案 1 :(得分:1)
感谢@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