cancel
Showing results for 
Search instead for 
Did you mean: 

How to make Excel VBA monitor SAP GUI Events?

former_member213011
Participant
0 Kudos

Dear Community,

Is it possible to make VBA monitor events raised by SAP GUI such as CreateSession or DestroySession event?

From the SAP GUI Scripting API, we know that this is possible in VBS and the procedure is pretty straightforward but VBA is not as straightforward as VBS.

I think Class Module is the way to go but so far I have not found any reference on how to connect to other objects which is not Office-related.

Any help or tips will be very much appreciated. In the mean time, I'll continue experimenting and will post if I've figure it out.

Thanks,

Sayuti

p/s: Sorry moderator if this is a redundant post, but due to bad internet connection, I'm not sure whether my earlier post on this topic went through or not.

Accepted Solutions (1)

Accepted Solutions (1)

former_member213011
Participant
0 Kudos

Figured it out: Using Class Module in SAP and early binding to SAPFEWSELIB reference.

Here's how I tested the GuiApplication event on a single connection with a single Session:

1. I created a Class Module in Excel VBE and name it clsSAP.

2. In the clsSAP module, I wrote the following scripts:

'-------------------------------------------------------clsSAP begin:

Option Explicit

Public sapAuto As Object

Public WithEvents sapGUI As SAPFEWSELib.guiapplication

Public sapCon As SAPFEWSELib.GuiConnection

Public WithEvents sapSession As SAPFEWSELib.GuiSession

Sub connectSAP()

    Set sapAuto = GetObject("sapgui")

    Set sapGUI = sapAuto.getScriptingEngine

    Set sapCon = sapGUI.Children(0)

    Set sapSession = sapCon.Children(0)

End Sub

Sub killSAP()

    Set sapAuto = Nothing

    Set sapGUI = Nothing

    Set sapCon = Nothing

    Set sapSession = Nothing

End Sub

'guiapplication event procedure when a session is created

Sub sapGUI_CreateSession(ByVal Session As GuiSession)

    MsgBox "GuiApp CreateSession Event Triggered with ID: " & Session.ID

End Sub

'guiapplication event procedure when session is closed

Sub sapGUI_DestroySession(ByVal theSession As GuiSession)

    MsgBox "GUIApp DestroySession Event Triggered with ID: " & theSession.ID

End Sub

'----------------------------------------------------------------------------- clsSAP end

3. In a regular VBA module Module1, I wrote the following script:

'---------------------------------------------------------------------- Module1 begin:

Option Explicit

Public mySAP As New clsSAP

Sub startEvent()

    mySAP.connectSAP

End Sub

Sub stopEvent()

    mySAP.killSAP

End Sub

'---------------------------------------------------------------------- Module1 ends:

4. I ran the startEvent procedure. Whenever a new session is created or closed, a message box will be prompted in Excel. This will continue until I run the stopEvent procedure or when the first SAP session with the running script is closed.

Hope this information will be useful to anyone else interested.

stefan_schnell
Active Contributor
0 Kudos

Hello Sayuti,

thank you very much for this very interesting information and the sharing of your source.

Cheers

Stefan

Former Member
0 Kudos

Which events can further be used beside Create and DestroySession?

former_member213011
Participant
0 Kudos

For GuiApplication Control:

- CreateSession

- DestroySession

- IgnoreSession

For GuiSession Object:

- Activated

- AutomationFCode

- Change

- ContextMenu

- Destroy

- EndRequest

- FocusChanged

- Hit

- StartRequest

For parameters and usage, you can refer to the SAP Scripting API help file.

former_member203905
Discoverer
0 Kudos

Hi all,

I found this interesting code today. I'd like to mention the name of Reference in VBA editor SAP GUI Scripting API located in C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx, or just try to search sapfewse.ocx in the Program Files folder (my location is little bit different).

former_member213011
Participant
0 Kudos

Dear Pavel,

I would suggest you just search for sapfewse.ocx.

0 Kudos

Hi,

Can I run the event code provided on the same time other module is making other commands on SAP?

Should I call the event code during the execution of the code or just on the beginning?

I can make them run one at a time, not both.

Answers (0)