Skip to Content
author's profile photo Former Member
Former Member

Need help with excel vba to run R/3 and APO both with the same macro

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Sep 08, 2013 at 02: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

    ‘-------------------------------------------------------------------------------

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.