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