0

I need some help with a macro in VBA. It connects to SAP GUI and performs a series of repetitive actions that would take days to be done manually.

I don't understand anything of the SAP GUI Scripting language. I need to know how I can use a variable instead of [1,0], so when the loop runs it uses the variable increment. The screen is SAP ECC IW32 for maintenance orders.

My knowledge in SAP GUI Scripting language is nil.

Below is the VBA code.

' Sua rotina continua aqui.
Sub CONEXÃO()
    Dim SAP, SAPGUI, Connections, cntConnection, i, Connection, RetVal
    'Parametros
    On Error GoTo erro
    Set SAP = GetObject("SAPGUI")
    Set SAPGUI = SAP.GetScriptingEngine()
    Set Connections = SAPGUI.Connections()
    cntConnection = Connections.Count()
    Set Connection = SAPGUI.Connections(CLng(i))

    'Check Conexão
    If IsObject(Connection) Then
        ConnectionExists = vbTrue
    Else
        ConnectionExists = vbFalse
erro:
        RetVal = Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")   ' Execute o Programa.
        Set xl = CreateObject("excel.application")
        While xl.Ready = False
        Wend

        Set SapGuiAuto = GetObject("SAPGUI")
        Set SapApp = SapGuiAuto.GetScriptingEngine
        Set Connection = SapApp.OpenConnection("01 [BRF] - SAP ECC Produção -->Logon Automático<---", True)
        Set SapCon = SapApp.Children(0)
        Set session = SapCon.Children(0)


        'session.findById("wnd[0]").maximize
        'session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "mandante"
        'session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "usuário"
        'session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "senha"
        'session.findById("wnd[0]").sendVKey 0

        'Set session = Nothing
        'connection.CloseSession ("ses[0]")
        'Set connection = Nothing
        'Set sap = Nothing
    End If
End Sub

Sub Macro1()
    Application.Run "CONEXÃO"

    Dim App, Connection, session As Object
    Set SapGuiAuto = GetObject("SAPGUI")
    Set App = SapGuiAuto.GetScriptingEngine
    Set Connection = App.Children(0)
    Set session = Connection.Children(0)

    Dim i, x As Long

    i = 2
    x = 0
    While Planilha1.Cells(i, "B") <> ""

        ORDEM = Planilha1.Cells(i, 2)
        CPP = Planilha1.Cells(i, 3)
        QUANTIDADE = Planilha1.Cells(i, 4)
        OPERACAO = Planilha1.Cells(i, 5)
        APROVADOR = Planilha1.Cells(i, 6)
        CONTA = Planilha1.Cells(i, 7)
        GESTOR = Planilha1.Cells(i, 8)
        TIPO = Planilha1.Cells(i, 9)
        CONTRATO = Planilha1.Cells(i, 10)
        ITEMC = Planilha1.Cells(i, 11)
        
        Do While i <= 2
            session.findById("wnd[0]").maximize
            session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw32"
            session.findById("wnd[0]").sendVKey 0
            session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").Text = ORDEM
            session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").caretPosition = 12
            session.findById("wnd[0]").sendVKey 0
            session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1100/tabsTS_1100/tabpMUEB").Select
            i = i + 1
        Loop
        session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/ctxtRESBD-MATNR[1,0]").Text = CPP
        session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/txtRESBD-MENGE[4,0]").Text = "1"
        session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/txtRESBD-MENGE[4,0]").SetFocus
        session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1101/tabsTS_1100/tabpMUEB/ssubSUB_AUFTRAG:SAPLCOMK:3020/tblSAPLCOMKTCTRL_3020/txtRESBD-MENGE[4,0]").caretPosition = 18
        session.findById("wnd[0]").sendVKey 0
        session.findById("wnd[1]/usr/txtRCM01-VORNR").Text = OPERACAO
        session.findById("wnd[1]/usr/txtRCM01-VORNR").caretPosition = 2
        session.findById("wnd[1]").sendVKey 0
        session.findById("wnd[1]/usr/ctxtVG_SAKNR").Text = CONTA
        session.findById("wnd[1]/usr/ctxtVG_SAKNR").caretPosition = 6
        session.findById("wnd[1]/tbar[0]/btn[13]").press
        session.findById("wnd[0]/usr/chkEBAN-WEUNB").Selected = False
        session.findById("wnd[0]/usr/txtEBAN-AFNAM").Text = "433004"
        session.findById("wnd[0]/usr/ctxtEBAN-BEDNR").Text = APROVADOR
        session.findById("wnd[0]/usr/ctxtEBAN-EKORG").Text = "0001"
        session.findById("wnd[0]/usr/ctxtEBAN-EKORG").SetFocus
        session.findById("wnd[0]/usr/ctxtEBAN-EKORG").caretPosition = 4
        session.findById("wnd[0]/tbar[0]/btn[0]").press
    Wend
3
  • 1
    Instead of "...-MATNR[1,0]" you would do something like "...-MATNR" & yourVariable. Commented Apr 5, 2023 at 20:24
  • For information, here you miss VBA knowledge about concatenating text and variable, not about SAP GUI Scripting. This question has been already asked and answered. Commented Apr 6, 2023 at 17:30
  • 2
    Does this answer your question? Concatenating a string and numeric variable Commented Apr 6, 2023 at 17:35

1 Answer 1

3

For example

session.findById("wnd[0]").maximize

Here "wnd[0]" is just a String, so you could do something like (eg)

Dim i As Long

i = 2
session.findById("wnd[" & i & "]").maximize

And if you needed to increment that in a loop:

For i = 0 To 5
    session.findById("wnd[" & i & "]").maximize
Next i

or multiple variables

"...-MATNR[1,0]" >> "-MATNR[" & i & "," & j & "]"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.