Cómo realizar un script en excel para SAP

Se necesitan 2 cosas tener activado los scripts en SAP y tener activado los macros en excel
envio foto de donde se encuentran los scripts en SAP

como pueden ver existe un boton que dice grabar y reproducir scripts le dan clic
y aparecera la siguiente imagen


ustedes daran click en grabar script y se meten a la transacción de la cual quieren hacer la plantilla
en este caso usaremos la f-03 en donde realizaremos nuestro trabajo normal con esta plantilla
y le daran en parar en la ventana de grabar y reproducir.
lo que hara una nueva ventana como esta

lo cual nos guardara un script lo cual se puede usar en excel para grabar lo que sea

la siguiente parte es la configuración de visual scripts en excel para lo cual en nuestro proyecto debemos de solicitar la referencia a un archivo que si tenemos instalado sap nos va a aprecer el cual es


y hacemos la conexión en un modulo

Public SapGuiAuto, WScript, msgcol
Public objGui  As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Dim W_System





Function Attach_Session() As Boolean
Dim il, it
Dim W_conn, W_Sess

If W_System = "" Then
   Attach_Session = False
   Exit Function
End If

If Not objSess Is Nothing Then
    If objSess.Info.SystemName & objSess.Info.Client = W_System Then
        Attach_Session = True
        Exit Function
    End If
End If

If objGui Is Nothing Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set objGui = SapGuiAuto.GetScriptingEngine
End If

For il = 0 To objGui.Children.Count - 1
    Set W_conn = objGui.Children(il + 0)
    For it = 0 To W_conn.Children.Count - 1
        Set W_Sess = W_conn.Children(it + 0)
        If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System Then
            Set objConn = objGui.Children(il + 0)
            Set objSess = objConn.Children(it + 0)
            Exit For
        End If
    Next
Next

If objSess Is Nothing Then
   MsgBox "No active session to system " + W_System + ", or scripting is not enabled.", vbCritical + vbOKOnly
   Attach_Session = False
   Exit Function
End If

If IsObject(WScript) Then
   WScript.ConnectObject objSess, "on"
   WScript.ConnectObject objGui, "on"
End If

Set objSBar = objSess.FindById("wnd[0]/sbar")
objSess.FindById("wnd[0]").Maximize
Attach_Session = True


End Function

Public Sub RunGUIScript()

On Error GoTo Salto
Dim error As Integer
Dim W_Ret As Boolean
Dim soldto, shipto As String
Dim x As Integer
Dim y As Integer


W_Ret = Attach_Session

If Not W_Ret Then
    Exit Sub
End If

 x = 0
 y = 10


objSess.FindById("wnd[0]/tbar[0]/okcd").Text = "/nf-03" '---- Transaccion
objSess.FindById("wnd[0]").sendVKey 0 '---- Enter

objSess.FindById("wnd[0]/usr/ctxtRF05A-AGKON").Text = Hoja2.Cells(2, 3).Value
objSess.FindById("wnd[0]/usr/ctxtBKPF-BUDAT").Text = Hoja2.Cells(3, 3).Value
objSess.FindById("wnd[0]/usr/txtBKPF-MONAT").Text = Hoja2.Cells(5, 3).Value
objSess.FindById("wnd[0]/usr/ctxtBKPF-BUKRS").Text = Hoja2.Cells(4, 3).Value
objSess.FindById("wnd[0]/usr/ctxtBKPF-WAERS").Text = Hoja2.Cells(6, 3).Value
objSess.FindById("wnd[0]/usr/sub:SAPMF05A:0131/radRF05A-XPOS1[2,0]").Select
objSess.FindById("wnd[0]/tbar[0]/btn[0]").press


Do While Not Hoja2.Cells(y, 2).Value = ""

objSess.FindById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").Text = Hoja2.Cells(y, 2).Value
objSess.FindById("wnd[0]").sendVKey 0

y = y + 1

Loop

objSess.FindById("wnd[0]/tbar[1]/btn[16]").press '---- Tratar pas
objSess.FindById("wnd[0]/mbar/menu[0]/menu[1]").Select '----- Simular

objSess.FindById("wnd[0]/usr/txtBKPF-XBLNR").Text = Hoja2.Cells(2, 6).Value
objSess.FindById("wnd[0]/usr/txtBKPF-BKTXT").Text = Hoja2.Cells(3, 6).Value


MsgBox ("Carga Completa...")

Salto:
If Err.Number <> 0 Then
If error = 0 Then
    Hoja2.Cells(2, 9).Value = objSess.FindById("wnd[0]/sbar").Text
    error = error + 1
End If
'Resume Next

End If



End Sub

Sub StartExtract()
Dim currentline As Integer

    ' This is the system to connect to
    W_System = "sistema de sap ejemplo ERQ300"


    ' Run the actual GUI script
    RunGUIScript
    
    'objSess.EndTransaction
   

End Sub

En la cual tenemos un excel de la siguiente manera

Ponemos enter y empieza la magia

7 Me gusta