cancel
Showing results for 
Search instead for 
Did you mean: 

Syslog Errors when script is executed

Former Member
0 Kudos

I have written my first SAP Script and it does exactly what I want.

I connect through Microsoft Excel (VBA) to SAP, run a transaction and with draw the information I want back into Excel.  Everything on the front end seems to work correctly. 

However, my SAP Admin told me he is getting a "Connection lost" error every time I connect.  Please see the error log...

Accepted Solutions (1)

Accepted Solutions (1)

holger_khn
Contributor
0 Kudos

May you can provide some code how you connect via VBA from Excel to SAP?

Is it session Scripting or call RFC function module via script?

Former Member
0 Kudos

Function getpartRev(inputPartNumber As String)

    Dim SapGuiApp As Object

    Dim oConnection As Object

    Dim SAPsession As Object

  

    If SapGuiApp Is Nothing Then

        Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1")

    End If

  

    If oConnection Is Nothing Then

        Set oConnection = SapGuiApp.OpenConnection("04 Sandbox - SBX - (SSO)/INPLACE", True)

    End If

  

    If SAPsession Is Nothing Then

       Set SAPsession = oConnection.Children(0)

    End If

    On Error GoTo ErrorHandler

    SAPsession.findbyid("wnd[0]").iconify

    SAPsession.findbyid("wnd[0]/tbar[0]/okcd").Text = "/nMM03"

    SAPsession.findbyid("wnd[0]").SendVKey 0

    SAPsession.findbyid("wnd[0]/usr/ctxtRMMG1-MATNR").Text = inputPartNumber

    SAPsession.findbyid("wnd[0]").SendVKey 0

  

    Dim outputText As String

    outputText = SAPsession.findbyid("wnd[0]/usr/tabsTABSPR1/tabpSP01/ssubTABFRA1:SAPLMGMM:2004/subSUB1:SAPLMGD1:1002/txtRMMZU-REVLV").Text

    getpartRev = outputText

  

    'oConnection.logoff

    'SAPsession.findbyid("wnd[0]").Close

  

ErrorHandler:

    Select Case Err

        Case 619 'Material does not exist in SAP

        getpartRev = ""

    End Select

  

End Function

I have tried uncommenting lines 33/34 with no success

holger_khn
Contributor
0 Kudos

Ok. With this code it´s quite visible why you have this syslog entries.

When you call function in a Loop every new step execution of your Queue will connect to SAP and create an session. As this object is only valid as long as function is active this session got disconnected by force when script exit function..

You should create an Sub procedure which open an SAP session and then Loop over your 'getpartRev' Input and use whole time this session.

Or adopt to an open session which you have already open. This is another Approach. Sessions created by SAPGUI.ScriptingCtrl.1 have an limited Screen.

Former Member
0 Kudos

Thanks Kohn,

I suspected this and tried to disconnect the session using lines 33/34 with no luck.  How could I disconnect the session before exiting the function to prevent the errors I have been seeing?

holger_khn
Contributor
0 Kudos

Use a global variable.

Run a seperate Function to create an session

Assign session to global variable

Then use global session variable in our GetPartRev-function

If you Exit your workbook or do not want use any longer session build a Switch or any similar method to logoff.

It´s VBA logic that created objects will terminate when function is finished.

So you call your function

This will create an SAP session

Execute some steps with session

Then exist function = Session got terminated

EDIT: May session.CloseSession will terminate and it will Close Connection as well.

Message was edited by: Holger Kohn Updated with method CloseSession

Answers (0)