问题描述
我目前正在尝试使用 VBA 修复 SAP 过程中的一些问题。
现在我有一段代码可以正确处理有时出现的错误弹出窗口:
If session.findById("wnd[2]/usr/txtMESSTXT1").Text = "Material já atualizado para esta operação" Then
'Saves error text
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i,"d") = "error"
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i,"e") = Now
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i,"f") = session.findById("wnd[2]/usr/txtMESSTXT1").Text
'Goes to the inicial SAP window
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nBPMDG/UTL_BROWSER"
session.findById("wnd[0]/tbar[0]/btn[0]").Press
'Goes to the last line of the while loop
GoTo end_while
End If
问题是这样的:这个错误是由我的数据库中的一些行触发的。如果我以这种方式保留代码,则会在不需要错误处理的行中导致错误。我需要验证活动会话是否是错误弹出窗口,然后继续处理错误,即,我需要这样的东西:
If ActiveSession.Name = "wnd[2]/usr/txtMESSTXT1" Then
If session.findById("wnd[2]/usr/txtMESSTXT1").Text = "Material já atualizado para esta operação" Then
'Saves error text
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i,"d") = "error"
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i,"e") = Now
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i,"f") = session.findById("wnd[2]/usr/txtMESSTXT1").Text
'Goes to the inicial SAP window
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nBPMDG/UTL_BROWSER"
session.findById("wnd[0]/tbar[0]/btn[0]").Press
'Goes to the last line of the while loop
GoTo end_while
End If
End If
但我不知道如何正确验证活动窗口是否是我正在使用的错误窗口。
感谢您对此语法的任何帮助。
解决方法
您可以使用以下 2 种方法来查找相关的 SAP 会话:
-
简单的方法
Set SapGuiAuto = GetObject("SAPGUI") Set SapApplication = SapGuiAuto.GetScriptingEngine Set connection = SapApplication.Children(0) set session = SapApplication.activeSession ... If session.Name = "wnd[2]/usr/txtMESSTXT1" Then ...
-
复杂的方法
Set SapGuiAuto = GetObject("SAPGUI") Set SapApplication = SapGuiAuto.GetScriptingEngine Set connection = SapApplication.Children(0) for mySession = 0 to connection.children.count - 1 Set ActiveSession = connection.Children(int(mySession)) If ActiveSession.Name = "wnd[2]/usr/txtMESSTXT1" Then set session = ActiveSession exit for end if next If session.Name = "wnd[2]/usr/txtMESSTXT1" Then ...
您必须自己找出这两种方法中的哪一种适用于您。
问候,脚本人