cancel
Showing results for 
Search instead for 
Did you mean: 

Request for a macro code that connects an open SAP application

Former Member
0 Kudos

Hi Script man,

I hope you're fine and Thanks for your last clarification for my quest.

Now I need a help from you. I don't know how to open a new post and also i tried to send a direct message to you. But I couldn't. Thats why I place this query here. Please help.

I 'm using the follwing (your) code to connect Excel & SAP and have created many automations. All of them are working very fine except one.

The SAP window closes after macro run is completed.

Could you please provide me a code which will not close SAP window after macro run is finished.

And also please provide me a code to transfer data from Excel to a opened (manually) SAP window.

Your timely reply will help me more to work out.

Thanks ScriptMan.

Code:

Set SAP = CreateObject("SAP.Functions")

                  
          Set conn = SAP.connection

       

If Not IsObject(SapGuiApp) Then

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


If Not IsObject(connection) Then

          Set connection = SapGuiApp.OpenConnection("051 - PRD - EP1", True)
End If


If Not IsObject(session) Then

         Set session = connection.Children(0)

End If

UserName = InputBox("Please Enter Your User Name")
Password = InputBox("Please Enter password")

session.findById("wnd[0]/usr/txtRSYST-BNAME").text = UserName
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = Password

session.findById("wnd[0]").sendVKey 0

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

I think the problem here is that when the program is done (you send an enter to the sapgui after the password is entered)

The session object depends on the connection and in the sapguiapp  object which are not global variables but they are local ones which are disposed after the macro is run.

Answers (1)

Answers (1)

holger_khn
Contributor
0 Kudos

Hello.

This is some code what can be used to connect to an open SAP session. I have removed some functions which I use as this require more functions in your Excel-VB-Project. So this is really simple but not as comfortable as my function enhancement make it for me.

'------------------------------Start Excel Sub--------------------------

Sub function_button1()

Dim sap_applic
Dim Connection
Dim session
Dim WScript
Dim iMSG1%
Dim iMSG2%
Dim a%
Dim b%

'**************************************************************************
'* set SAP session                                                        *
'**************************************************************************

If Not IsObject(sap_applic) Then

   Dim SapGuiAuto

   On Error Resume Next

   Set SapGuiAuto = GetObject("SAPGUI")

   On Error GoTo 0

   If SapGuiAuto Is Nothing Then

        MsgBox "Please start SAPlogon"

        Exit Sub

   End If

   On Error Resume Next

   Set sap_applic = SapGuiAuto.GetScriptingEngine

   On Error GoTo 0

   If sap_applic Is Nothing Then

        MsgBox "Scripting disabled"

        Exit Sub

    End If

End If

If Not IsObject(Connection) Then
    For a = 0 To (sap_applic.Children.Count - 1)
        For b = 0 To 6
            On Error Resume Next
                iMSG1 = MsgBox("Window: " & sap_applic.Children(0 + a).Children(0 + b).info.sessionnumber & "| System: " & sap_applic.Children(0 + a).Children(0 + b).info.systemname & " | Transaction: " & sap_applic.Children(0 + a).Children(0 + b).info.transaction & " | User: " & sap_applic.Children(0 + a).Children(0 + b).info.user & vbCr & "Do you want to use this session?", vbQuestion + vbYesNo + vbDefaultButton2, "SAP Session")
                If iMSG1 = "6" Then
                    Set Connection = sap_applic.Children(0 + a)
                    If Not IsObject(session) Then
                        Set session = Connection.Children(0 + b)
                    End If
                    Exit For
                End If

        Next b
            If iMSG1 = "6" Then Exit For
    Next a
End If

If sap_applic.Children.Count = "0" Then
    MsgBox "Please logon to SAP-System where you want use an session!", vbCritical + vbMsgBoxSetForeground, "Error"
    Exit Sub
End If

If iMSG1 = "7" Then
        MsgBox "You have not selected any available SAP Session. Execution aborted!", vbInformation + vbMsgBoxSetForeground, "Exit Information"
        Exit Sub
Else
        iMSG2 = MsgBox("You have selected System: " & session.info.systemname & " | Open transaction: " & session.info.transaction & " | Executed by user: " & session.info.user, vbInformation + vbMsgBoxSetForeground + vbYesNo + vbDefaultButton2, "System Information")
End If

If iMSG2 = "7" Then
    Set session = Nothing
    Set Connection = Nothing
    Set sap_applic = Nothing
    Set SapGuiAuto = Nothing
    MsgBox "You have aborted this update. Please start again.", vbExclamation + vbMsgBoxSetForeground, "Information"
    Exit Sub
End If

If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject sap_applic, "on"
End If

'------------------------Start of your Code----------------------------------------------

Msgbox "Ready for your code"

'------------------------End of your Code-----------------------------------------------

    Set session = Nothing
    Set Connection = Nothing
    Set sap_applic = Nothing
    Set SapGuiAuto = Nothing

End Sub

'------------------------------Start Excel Sub--------------------------

Hope this will help you to achieve your requirements.

Br, Holger