Caue Evangelista
Caue Evangelista

Reputation: 23

How to verify the Active Window in SAP GUI using VBA

I'm currently trying to fix some issues in a SAP procedure using VBA.

By now I have a piece of code that can correctly handle the error pop-up that sometimes appears:

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

The issue is this: this error is triggered by just some of the lines of my database. If I leave the code this way, it will cause an error in the lines that don't need the error handling. I need to verify if the active session is the error pop-up and then proceed to handle the error, ie, I need something like this:

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

but I don't know how to properly verify if the active windows is THE error window I'm working with.

I appreciate any help with this syntax.

Upvotes: 1

Views: 5741

Answers (2)

Stracke
Stracke

Reputation: 1

In these cases I use the VBA property "session.ActiveWindow.Name". Using that you'll find out the current SAP active window number. Remember, the first SAP window number is indexed as "0"(wnd[0]), the second one is "1"(wnd[1]) and so on.

Let me know if you get the value you was looking for.

Upvotes: 0

ScriptMan
ScriptMan

Reputation: 1625

You could use the following 2 methods to find the relevant SAP session:

  1. the easy way

     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. the complicated method

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

You will have to find out for yourself which of the two methods is applicable to you.

Regards, ScriptMan

Upvotes: 2

Related Questions