Vba sap gui scripting choose specific session

做~自己de王妃 提交于 2020-02-03 01:50:50

问题


I am trying to run an Excel VBA code which uses SAP GUI Scripting. I have some open sessions in Sap and using two systems "F6P" and "FVP" at the same time.

How to run on one of the "FVP" sessions? You can see below there are two session open One is F6P SAP box and the other is FVP SAP box.

    Option Explicit
    Public SapGui, App, Connection, Session, SapGuiAuto, WScript

    Sub Overconfirmation()
        Call SAP
        Call tcode
        End
        Sub SAP()
        If Not IsObject(App) Then
           Set SapGuiAuto = GetObject("SAPGUI")
           Set App = SapGuiAuto.GetScriptingEngine
        End If
        If Not IsObject(Connection) Then
           Set Connection = App.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 App, "on"
           Set Session = Application.ActiveSession
        End If
    End Sub

    Sub tcode()
        Session.findById("wnd[0]").maximize
        Session.findById("wnd[0]/tbar[0]/okcd").Text = "/n/sapapo/bopin"
        Session.findById("wnd[0]").sendVKey 0
    end sub  

Thank you.


回答1:


The ID of the SAP system is defined in the property SystemName of the object GuiSessionInfo, which itself is the property Info of the object GuiSession. You may access all the existing sessions with this code:

For connNr = 0 To Application.Children.Length - 1
  Set conn = Application.Children.ElementAt(connNr)
  For sessNr = 0 to conn.Children.Length - 1
    Set sess = conn.Children.ElementAt(sessNr)
    If sess.Busy = False Then
      ' Test Busy because property Info is blocked when the session runs something
      If sess.Info.SystemName = "FVP" Then
        ' Do whatever you want with session sess
      End If
    End If
  Next
Next


来源:https://stackoverflow.com/questions/59832774/vba-sap-gui-scripting-choose-specific-session

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!