VBA

Automação SAP com VBA

Sub CriarME01()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("LOF")

    Dim ultimaLinha As Long
    ultimaLinha = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    If Not IsObject(Appl) Then
        Set SapGuiAuto = GetObject("SAPGUI")
        Set Appl = SapGuiAuto.GetScriptingEngine
    End If

    session.FindById("wnd[0]/tbar[0]/okcd").Text = "/n me01"
    session.FindById("wnd[0]").sendVKey 0

    For i = 2 To ultimaLinha
        On Error Resume Next
        session.FindById("ctxtEORD-MATNR").Text = ws.Cells(i, 1).Value
        session.FindById("ctxtEORD-LIFNR").Text = ws.Cells(i, 3).Value
        session.FindById("wnd[0]").sendVKey 11

        If typeMessage <> "S" Then
          ws.Cells(i, "G").Value = "Erro"
        Else
          ws.Cells(i, "G").Value = "Sucesso"
        End If
    Next i

End Sub