07-20-2013 4:24 AM
I am trying to make my existing excel vba macro that works fine with R/3 already, also switch windows to manipulate an APO session that I have already logged into
.
How do I select/change the SAP window that the macro is controlling?
Any help or advice would be greatly appreciated!
Mike
09-08-2013 3:44 AM
Well after many hours of internet surfing and a 6 pack I did find a solution that worked for my application. It may not be the prettiest, but it does work and I can switch between the windows (no matter what order they were opened in) at will.
Below is what I pieced together.
Please feel free to critique as I am very new to SAP and VBA both and have lots to learn.
Option Explicit
Sub example()
Dim Application, SapGuiAuto, Connection, session
Dim SID, bh1Wnd, ba1Wnd, CollCon, i, CollSes
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
Set CollCon = Application.Connections()
If Not IsObject(CollCon) Then
Exit Sub
End If
'- Find APO and R3 windows -------------------------------------------
For i = 0 To CollCon.Count() - 1
Set Connection = Application.Children(CLng(i))
If Not IsObject(Connection) Then
Exit Sub
End If
Set CollSes = Connection.sessions()
If Not IsObject(CollSes) Then
Exit Sub
End If
Set session = Connection.Children(0)
SID = session.info.SystemName()
If Not IsObject(bh1Wnd) Then
If SID = "BH1" Then Set bh1Wnd = Application.Children(CLng(i)) 'R3
End If
If Not IsObject(ba1Wnd) Then
If SID = "BA1" Then Set ba1Wnd = Application.Children(CLng(i)) 'APO
End If
Next
'-Change to R3 window---------------------
Set Connection = bh1Wnd
Set session = Connection.Children(0)
‘-------------------------------------------------------------------------------
‘code to do various tasks in R3 goes here
‘-------------------------------------------------------------------------------
'----- change to APO window------------------------------------------------------
Set Connection = ba1Wnd
Set session = Connection.Children(0)
‘-------------------------------------------------------------------------------
‘code to do various tasks in APO goes here
‘-------------------------------------------------------------------------------