mercoledì 19 ottobre 2016

Mostra blocchi SAP da file Excel

L'intento è quello di avere un file Excel con un pulsante che consente di collegarsi, tramite macro Vba, ad un sistema SAP, leggere i blocchi attivi (le tabelle bloccate) e riportarle poi nel foglio.

Collegare il pulsante alla macro:
Sub main()
Dim R3 As Object

Set R3 = CreateObject("SAP.Functions")


zlogin R3


readSM12 R3


End Sub


Sub zlogin (prende i dati di accesso dal foglio Logon)
Sub zlogin(R3 As Object)

R3.Connection.ApplicationServer = Sheets("Logon").Cells(1, 2).Value

R3.Connection.SystemNumber = Sheets("Logon").Cells(2, 2).Value
R3.Connection.User = Sheets("Logon").Cells(3, 2).Value
R3.Connection.Password = Sheets("Logon").Cells(4, 2).Value
R3.Connection.client = Sheets("Logon").Cells(5, 2).Value
R3.Connection.Language = Sheets("Logon").Cells(6, 2).Value

'Finally, try to logon to the specified system and check if the connection established

If R3.Connection.Logon(0, True) <> True Then
  MsgBox "Cannot Log on to SAP" 'Issue message if cannot logon
  Exit Sub
Else
  'MsgBox "Logged on to SAP!"
End If

End Sub
 Infine il codice con per chiamare 2 funzioni standard:
ENQUEUE_REPORT
BAPI_USER_GET_DETAIL
in pratica ci si collega al sistema R3 e si vanno a leggere i blocchi di sistema (come con transazione SM12), poi per ogni riga legge il dettaglio dell'utente riportando nome, cognome e reparto.
Sub readSM12(R3 As Object)

' Cancello dati precedenti

Sheets("SM12").Select
Range("A2:M500").Select
Selection.ClearContents
Range("A2:A2").Select
    
' Definizione funzione per leggere LOCK
Set myFuncMTE = R3.Add("ENQUEUE_REPORT")

'Definizione per BAPI_USER_GET_DETAIL

Set myFuncGETDET = R3.Add("BAPI_USER_GET_DETAIL")

Dim Pusername, Pusername2, PAddress, Preturn As Object

Set Pusername = myFuncGETDET.exports("USERNAME")
Set Pusername2 = Pusername
Set PAddress = myFuncGETDET.imports("ADDRESS")
Set Preturn = myFuncGETDET.tables("RETURN")

' loading SAP functions input parameters

Dim ZCLIENT, ZNAME, ZTARG, ZUNAME As Object
    Set ZCLIENT = myFuncMTE.exports("GCLIENT")
        ZCLIENT.Value = Sheets("Logon").Cells(5, 2).Value
    Set ZNAME = myFuncMTE.exports("GNAME")
        ZNAME.Value = ""
    Set ZTARG = myFuncMTE.exports("GTARG")
        ZTARG.Value = ""
    Set ZUNAME = myFuncMTE.exports("GUNAME")
        ZUNAME.Value = ""
' calling SAP Func
    If myFuncMTE.call = False Then
           MsgBox myFuncMTE.Exception
    End If
' Passing SAP output to Excel variables
    Set ENQ = myFuncMTE.tables("ENQ")
    Set Number = myFuncMTE.imports("NUMBER")
    
    Pusername2 = ""
    
    For j = 1 To Number
    
              Sheets("SM12").Cells(j + 1, 1) = ENQ.Value(j, "GCLIENT")
              Sheets("SM12").Cells(j + 1, 2) = ENQ.Value(j, "GUNAME")
              Sheets("SM12").Cells(j + 1, 6) = ENQ.Value(j, "GTDATE")
              Sheets("SM12").Cells(j + 1, 7) = ENQ.Value(j, "GTTIME")
              Sheets("SM12").Cells(j + 1, 8) = ENQ.Value(j, "GTCODE")
              Sheets("SM12").Cells(j + 1, 9) = ENQ.Value(j, "GMODE")
              Sheets("SM12").Cells(j + 1, 10) = ENQ.Value(j, "GNAME")
              Sheets("SM12").Cells(j + 1, 11) = ENQ.Value(j, "GARG")
              Sheets("SM12").Cells(j + 1, 12) = ENQ.Value(j, "GUSE")
              Sheets("SM12").Cells(j + 1, 13) = ENQ.Value(j, "GUSEVB")
              
              Pusername.Value = ENQ.Value(j, "GUNAME")
              
             'Chiamo la funzione per leggere dettagli utente solo se non già chiamata
              If Pusername <> Pusername2 Then
               
                Pusername2 = Pusername
              
                'chiamo funzione per leggere i dati utente
                If myFuncGETDET.call = False Then
                  MsgBox ("Error Calling Function BAPI_USER_GET_DETAIL")
                End If
                
              End If

              Sheets("SM12").Cells(j + 1, 3) = PAddress.Value("FIRSTNAME")

              Sheets("SM12").Cells(j + 1, 4) = PAddress.Value("LASTNAME")
              Sheets("SM12").Cells(j + 1, 5) = PAddress.Value("department")

    Next

    
    Set myFuncMTE = Nothing
    Set myFuncGETDET = Nothing
    
End Sub 
file di esempio: SM12.xls

Note:

si devono compilare nel foglio "Logon" i dati di accesso al sistema SAP; in genere includo le informazioni nel codice VBA e poi lo proteggo con una password poichè altrimenti sono in chiaro. Anche così non è che sia una protezione molto solida, ma mi sembra il minimo.

nel PC deve essere installata la SAPGUI (che contiene le DLL per poter attivare il collegamento).

se nel server SAP è attivo il firewall deve essere aperta in ingresso la porta tcp 33nn (nn=<system number>), normalmente per le connessioni sapgui è aperta la 32nn.

consiglio di definire un utente di tipo S (servizio) che, in pratica, non può effettuare il logon da GUI.

Nessun commento:

Posta un commento