cancel
Showing results for 
Search instead for 
Did you mean: 

WSAECONNRESET - How can I get this error on VBA or VBS Scripts

0 Kudos

Hi, I am simulating the following:

Open one or more SAP session and afterwards switch off the connection (with can happen)
System will pop up the Message Box: WSAECONNRESET: Conection Reset by Peer.

When I run any VBA or VBS Script it looks "OK" because system still considering the SAP Logon Window Opened and also a SAP session active.

Basically all objects (I am considering) are still valid.

Do you know any way to get this error on in order to kill SAP run (like in task manager) or any order way?
and restablish a new conection?

I am using an example got from StackOverflow, but I have tested many others with similar results"

Exemple here:

Sub sap()

If SAP_Connection Then
MsgBox ("Sap is Open so just attached to session(0)")
Else
MsgBox ("Sap is NOT open so open Logon Window")
End If

End Sub
Function SAP_Connection() As Boolean

On Error GoTo ErrSap


If Not IsObject(SapApplication) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set SapApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = SapApplication.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 SapApplication, "on"
End If
   Set session = Nothing
SAP_Connection = True
Exit Function


ErrSap:
SAP_Connection = False

End Function

Thanks and appreciate any help on this.

Accepted Solutions (0)

Answers (1)

Answers (1)

rspecht
Explorer

You can use the DestroySession Even.

This event is raised before a session is destroyed . This can be done either by closing the main window manually, or by calling the closeSession method of GuiConnection.

Syntax
Visual Basic

Public Event DestroySession( _
 ByVal Session As GuiSession _
)
 

Example
You can handle this event from VBScript by adding the following procedure:

Dim objSapGui
Set objSapGui = GetObject("SAPGUI")

Dim objScriptingEngine
Set objScriptingEngine = 
objSapGui.GetScriptingEngine
WScript.ConnectObject objScriptingEngine, "Engine_"

Dim Waiting
Waiting = 1

Do While (Waiting = 1)
 WScript.Sleep(100)
Loop  
 
Set objScriptingEngine = Nothing
Set objSapGui = Nothing
 
Sub 
Engine_CreateSession(ByVal Session)
 Dim result
 result = MsgBox("Session created", vbOKCancel)
 If result = vbCancel then
   Waiting = 0
 End If
End Sub

Sub Engine_DestroySession(ByVal Session)
 Dim result
 result = MsgBox("Session destroyed",vbOKCancel)
 If result = vbCancel then   
  Waiting = 0
 End If
End Sub

0 Kudos
Thanks rspecht,
Your proposal works very well during an existent session, when an user manually closes or call a close event, however this event is not triggered when internet breaks its connection.

I would like to know if there is a way to get similar event when I already have an existent destroyed/broken connection and screen is blocked by the pop up message "WSAECONNRESET: Conection Reset by Peer".I would like to popose a test, in order to simulate this scenario, if you don't mind.
  • Open a SAP session.
  • Close the Internet connection
  • A message Box should be created with the following message: "WSAECONNRESET: Conection Reset by Peer".
Without close this pop up message, try to run any VBA or VBS?

During my several tests script still thinks I have a valid connection, maybe because a valid screen is still opened, blocked by the message box.

Maybe, thinking a little bit I can overcome this issue by checking first if there are this kind of pop up message to be closed first and try to create a new session in case it's found. But I think this should not be a good option as I expect some SAP api to cover it.
Thanks!