Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

excel - How to choose specific SAP GUI session?

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 do I 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.
enter image description here

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  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...