SAP Logon with VBA

-3

Good morning everyone!

I've been trying for a few days but I have not been able to get close: I'm trying to generate a VBA that:

  • Log in to SAP.
  • Perform a particular transaction.
  • Export to Excel.
  • But even the "log in to SAP" part did not succeed.

    I have tried several codes, the below until the login screen opens, but does not fill in the fields. I used CreateObject("Sapgui.ScriptingCtrl.1") :

    Sub Entrar_SAP()
    
    If Not IsObject(SAPguiApp) Then
    Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
    End If
    If Not IsObject(Connection) Then
    Set Connection = SAPguiApp.OpenConnection("xxxxxxx)", True)
    End If
    If Not IsObject(session) Then
    Set session = Connection.Children(0)
    End If
    session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"     
    session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "user"     
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "pass" 
    session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "PT" 
    session.findById("wnd[0]/usr/txtRSYST-LANGU").SetFocus     
    session.findById("wnd[0]/usr/txtRSYST-LANGU").caretPosition = 2 
    session.findById("wnd[0]").sendVKey 0
    End Sub
    

    However, another, comCreateObject("SAP.Functions") , resulted in the error: "RFC error received." In RFC authorization for RFC PING function module

    The code is:

    Declaration
    Dim objBAPIControl As Object 'Function Control (Collective object)
    Dim sapConnection As Object 'Connection object
    Set objBAPIControl = CreateObject("SAP.Functions")
    Set sapConnection = objBAPIControl.Connection
    sapConnection.Client = "xxxxx" 
    sapConnection.User = "xxxxxx"
    sapConnection.Language = "PT" 
    sapConnection.hostname = "xxxxx"
    sapConnection.Password = "xxxxxxxx" 'Fake password         
    sapConnection.SystemNumber = "4"
    sapConnection.System = "xxxxxx)"
    sapConnection.Logon If sapConnection.Logon(1, True) <> True Then
    MsgBox "No connection to R/3!"
    Exit Sub
    'End program 
    End If
    

    Can anyone help me with this, please? Many thanks!

        
    asked by anonymous 22.06.2015 / 16:49

    2 answers

    0

    Friend I connect this way:

    Sub retrieve_table_contents() 
    
    Dim R3**texto em negrito** As Object
    Set R3 = CreateObject("SAP.Functions")
    
    R3.Connection.System = ""
    R3.Connection.client = ""
    R3.Connection.user = ""
    R3.Connection.password = ""
    R3.Connection.language = "PT"
    
    If R3.Connection.logon(0, False) <> True Then
       Exit Sub
    End If
    
    End Sub
    
        
    16.07.2015 / 19:16
    0

    Good afternoon, Buddy, I have a source code that will get you into the transaction, however, after you enter the transaction, you need to save a SCRIPT from SAP itself and copy it right after the code below:

    \LOGIN SAP: *COMENTARIO <- NÃO COPIAR*
    
    Set SapGuiAuto = GetObject("SAPGUI")          'Utiliza o objeto da interface gráfica do SAP
    Set SAPApp = SapGuiAuto.GetScriptingEngine    'Conecta ao SAP que está rodando no momento
    Set SAPCon = SAPApp.Children(0)               'Encontra o primeiro sistema que está conectado
    Set session = SAPCon.Children(0)              'Encontra a primeira sessão (janela) dessa conexão
    
    If Not IsObject(Application) Then
       Set SapGuiAuto = GetObject("SAPGUI")
    End If
    If Not IsObject(Connection) Then
    End If
    If Not IsObject(session) Then
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session, "on"
       WScript.ConnectObject Application, "on"
    End If
    session.findById("wnd[0]").maximize
    
    Rem ***************************************************************************
    Rem BLOCO A : Inserir depois da setença maximize
    Rem Gerar o SCRIPT usando a chamada da transação com "/nxxxx"
    Rem ***************************************************************************
    
    Dim objExcel
    Dim objSheet, intRow, aux, Log
    Set objExcel = GetObject(, "Excel.Application")
    Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
    
    Rem Grava LOG******************************************************************
    Set objShell = CreateObject("Wscript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    With aux = 2 And objSheet.UsedRange.Rows.Count   'Assuming there is a header row
    
    Rem Find a cell and put its value in:
    Rem Note: The section of the Script shown below can be recorded and edited to pick data from the Excel sheet
    
    DataInicial = Range("H2") 'ColumnC
    DataFinal = Range("H3") 'ColumnC
    
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/xxxxx"
    
    \COMENTARIO***
    

    This part /xxxxx

    It is the transaction that I am entering, in case you are going to put the one you use, and after placing this, you must copy the script that you generated in SAP,

    I hope I have helped ...

        
    27.10.2017 / 20:58