Pages

Tuesday, 12 March 2013

How to login to SAP using Excel Macro

Note : 

I had posted these on SCN / SDN but because of some spammers, all my blogs on SCN were removed or hidden. Now even I cannot see them

So I am re-writing these... hope it helps people


How to login to SAP using Excel Macro

Introduction
As a BASIS admin, one has to constantly monitor SAP Servers. Compile many reports example getting profile parameters from RZ10 and compare with EWA report.
Database and hardware planning, In all cases we perform same tasks over and over again on several servers.....

SAP has provided RFC connection can me made using VB script, C program etc. I used this concept and use macros in excel to perform
     1. Login to SAP through RFC
     2. Monitor SAP servers
     3. Calling SAP GUIscripts

In this part I will give examples on how to login to SAP

Concept
There are mainly 2 types of logins ( There are many ways, I like the 2 below )
     1. Funtion Control : This utilizes the ActiveX module in wdtfuncs.OCX which part of your SAPGUI, We can pass server details,user and password in the code, which makes it very easy to login to SAP faster and be used in batch mode
     2. Logon Control :  This utilizes the ActiveX module in wdtlog.OCX which also part of your SAPGUI, We can select SAP server as in SAPlogon pad and provide user name and password on execution, best suited for fetching data for one server at a time, usng SNC, etc

Sample Code
Using Function Control  

Sub zlogin()
'Logon
    Dim R3 As Object 'Declare variant
    Set R3 = CreateObject("SAP.Functions") 'Create ActiveX object
    ' R3.Connection.SystemID = mysid
    R3.Connection.User = "myuserid" 'Specify user
    R3.Connection.Password = "mypass" 'Then password
    R3.Connection.client = "myclnt" 'Client
    R3.Connection.ApplicationServer = "myserver" 'Target server hostname or IP address
    R3.Connection.Language = "EN" 'Language code
'Finally, try to logon to the specified system and check if the connection established
    If R3.Connection.Logon(0, False) <> True Then
              MsgBox "Cannot Log on to SAP" 'Issue message if cannot logon    
    Else
              MsgBox "Logged on to SAP!" 
    End If
End SUB

Using Logon Control  
Sub zLogin()
   Dim Logon As Object 'Logon control
   Dim R3conn As Object
   Set Logon = CreateObject("SAP.LogonControl.1") 'Create ActiveX object
   Set R3conn = Logon.NewConnection
   If R3conn.Logon(0, False) <> True Then 'Try Logon
              MsgBox "Cannot log on to SAP"
   End If
End Sub


Summary
Once we have a successfully login to SAP, we can call remote enabled functions, bapi etc. I will cover how to call functions using RFC in my next blog. My favorite is the funtion control, it is very easy to set server details and call sap functions

Please let me know if you find errors in this document

6 comments:

  1. Please help. The program is supposed to pick the values on excel row by row. The variable i changes however the values picked for the logon credentials are always from row 2 (which is the initial value). Anything wrong with the code?

    Sub Z_LOGIN()

    Dim LogonControl As Object
    Dim conn As Object
    Dim retcd As Boolean
    Dim i As Integer

    Set LogonControl = CreateObject("SAP.LogonControl.1")
    Set conn = LogonControl.NewConnection


    For i = 2 To 5

    conn.ApplicationServer = Sheet1.Cells(i, 1).Value
    conn.System = Sheet1.Cells(i, 2).Value
    conn.Client = Sheet1.Cells(i, 3).Value
    conn.Language = "EN"
    conn.User = Sheet1.Cells(i, 4).Value
    conn.Password = Sheet1.Cells(i, 5).Value
    retcd = conn.Logon(0, True)


    If retcd <> True Then
    Sheet1.Cells(i, 6).Value = "Cannot Login"
    Else
    Sheet1.Cells(i, 6).Value = "OK"
    End If

    Next i

    End Sub

    ReplyDelete
  2. Add conn = null or nothing and reinitialize it

    ReplyDelete
  3. Hi Amit,
    I'd like to logon to SAP BW by using the excel macro.
    Can I use the same script for logon on SAP BW system? Logon control function?
    Which adjustmens are necessary?
    best regards and many thanks for your response

    ReplyDelete
  4. Hi

    I am able to login to SAP thru Excel by using Logon control. Would like to put a check whether the same user is already logged in SAP system , if present to flash message that the Login is used by another user and come out from Macro Sub routine.

    ReplyDelete
    Replies
    1. Try different user or logout from GUI. What is the excel and GUI version you agree using

      Delete
    2. I am using SAP Gui 7.6 and MS Excel 2013 32 bit. Want to avoid the multiuser logins .

      Delete