SAP GUI VBA脚本中设置Application.DisplayAlerts报错,求覆盖Excel替代方案
解决SAP GUI脚本VBA中Excel覆盖提示的问题
问题原因
你代码里把Application定义成了SAP脚本引擎对象,和Excel内置的Application对象重名,导致执行Application.DisplayAlerts = False时,VBA找不到Excel的Application对象,触发424错误。
三种可行解决方法
1. 明确指定Excel的Application对象
直接用Excel.Application.DisplayAlerts调用,绕开变量名冲突:
Excel.Application.DisplayAlerts = False ' 保存完成后建议恢复提示,避免影响后续操作 Excel.Application.DisplayAlerts = True
2. 提前删除目标文件(如果存在)
在执行SaveAs前检查文件是否存在,存在则删除,这样保存时不会触发覆盖提示:
Dim savePath As String savePath = "C:\Users\Cxxxx\Desktop\test.xlsx" ' 检查文件是否存在,存在则删除 If Dir(savePath) <> "" Then Kill savePath End If ' 执行保存操作 ActiveWorkbook.SaveAs Filename:=savePath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
3. 重命名SAP的Application变量(长期推荐)
把SAP脚本引擎的变量名改成SapApplication,彻底避免和Excel内置对象混淆,后续写代码更不容易出错:
Dim SapApplication, SapGuiAuto As Object ' 重命名变量 If Not IsObject(SapApplication) Then Set SapGuiAuto = GetObject("SAPGUI") Set SapApplication = SapGuiAuto.GetScriptingEngine End If If Not IsObject(Connection) Then Set Connection = SapApplication.Children(0) ' 同步修改引用 End If
修改后的完整代码
结合方法1和方法3,调整后的代码如下:
Sub CM02() ' 关闭Excel的覆盖提示 Excel.Application.DisplayAlerts = False Dim SapApplication, SapGuiAuto As Object If Not IsObject(SapApplication) Then Set SapGuiAuto = GetObject("SAPGUI") Set SapApplication = SapGuiAuto.GetScriptingEngine End If If Not IsObject(Connection) Then Set Connection = SapApplication.Children(0) End If If Not IsObject(session) Then Set session = Connection.Children(0) End If If IsObject(WScript) Then WScript.ConnectObject session, "on" WScript.ConnectObject SapApplication, "on" End If session.findById("wnd[0]").resizeWorkingPane 126, 39, False session.findById("wnd[0]/tbar[0]/okcd").Text = "/ncm02" session.findById("wnd[0]").sendVKey 0 session.findById("wnd[0]/mbar/menu[0]/menu[4]/menu[0]").Select session.findById("wnd[1]/usr/ctxtRC65A-PROFIL_ID").Text = "Z13_B-730T" session.findById("wnd[1]/usr/ctxtRC65A-PROFIL_ID").caretPosition = 10 session.findById("wnd[1]").sendVKey 0 session.findById("wnd[0]/mbar/menu[0]/menu[4]/menu[3]").Select session.findById("wnd[1]/usr/ctxtRC65A-LISPROF_ID").Text = "ZPP_TEST" session.findById("wnd[1]/usr/ctxtRC65A-LISPROF_ID").caretPosition = 8 session.findById("wnd[1]").sendVKey 0 session.findById("wnd[0]/usr/txt[35,3]").Text = "*" session.findById("wnd[0]/usr/txt[35,7]").Text = "1300" session.findById("wnd[0]/usr/txt[35,7]").SetFocus session.findById("wnd[0]/usr/txt[35,7]").caretPosition = 4 session.findById("wnd[0]").sendVKey 0 session.findById("wnd[0]/tbar[1]/btn[6]").press session.findById("wnd[1]/tbar[0]/btn[0]").press session.findById("wnd[1]/tbar[0]/btn[0]").press session.findById("wnd[1]/tbar[0]/btn[0]").press session.findById("wnd[1]/tbar[0]/btn[0]").press session.findById("wnd[0]").resizeWorkingPane 128, 39, False session.findById("wnd[0]/mbar/menu[4]/menu[11]/menu[1]/menu[1]").Select session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").SetFocus session.findById("wnd[1]/tbar[0]/btn[0]").press session.findById("wnd[1]/tbar[0]/btn[0]").press Windows("Tabelle von Basis (1)").Activate ActiveWorkbook.SaveAs Filename:="C:\Users\Cxxxx\Desktop\test.xlsx", _ FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False Windows("Tabelle von Basis (1)").Close ' 恢复Excel的提示设置 Excel.Application.DisplayAlerts = True End Sub
内容的提问来源于stack exchange,提问作者Carl
相关产品推荐
相关产品推荐

