如何使用 VBA 验证 SAP GUI 中的活动窗口

问题描述 投票:0回答:2

我目前正在尝试使用 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

但我不知道如何正确验证活动窗口是否是我正在使用的错误窗口。

我感谢任何有关此语法的帮助。

vba error-handling sap-gui
2个回答
2
投票

您可以使用以下2种方法来查找相关的SAP会话:

  1. 最简单的方法

     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   
     ...
    
  2. 复杂的方法

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

您必须自己找出这两种方法中哪一种适合您。

问候,ScriptMan


0
投票

我不记得在哪里找到解决方案。我不敢说它来自这里:“stackoverflow”。

请尝试属性“session.ActiveWindow.guifocus.ID”,然后您将捕获当前活动的 SAP 窗口。

如果您获得了所需的价值,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.