on 09-24-2013 5:18 PM
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.