I'm writing code to post/create PO's in ME23N by copying some data from an Excel looping.
The problem is that, when I try to run the script from the first Notebook[A] on the other [B], the second one gives the message: "The control could not be found by id".
Is there a way to use a variable for the "findById" code, so wherever I run it, it will copy data from Excel and paste it to a specific field?
"On error Go To 0" is not working because even with the error, I still need the information pasted to the field.
Below you can see my code.
Sub CTM()
If Not IsObject(Applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Applic = SapGuiAuto.GetScriptingEngine
'session.findById("wnd[0]").sendVKey 0
End If
If Not IsObject(Connection) Then
Set Connection = Applic.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Applic, "on"
End If
Dim num_linhas
Dim lin_atual
'Conta quantidade linhas
num_linhas = Worksheets("Base").Cells(Rows.Count, 3).End(xlUp).Row
'MsgBox (num_linhas - 1)
'Pega o numero da primeira linha cujo valor é diferente de zero
For lin_atual = 2 To num_linhas
'Abrir transação SAP
session.findById("wnd[0]/tbar[0]/okcd").Text = "ME23N"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[6]").press
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/ctxtMEPO_TOPLINE-SUPERFIELD").Text = "2000031572" ' FORNECEDOR
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/ctxtMEPO_TOPLINE-SUPERFIELD").SetFocus
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/ctxtMEPO_TOPLINE-SUPERFIELD").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT3").Select
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT3/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1230/subTEXTS:SAPLMMTE:0100/subEDITOR:SAPLMMTE:0101/cntlTEXT_EDITOR_0101/shellcont/shell").Text = Cells(lin_atual, 17) 'TEXTO
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT3/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1230/subTEXTS:SAPLMMTE:0100/subEDITOR:SAPLMMTE:0101/cntlTEXT_EDITOR_0101/shellcont/shell").setSelectionIndexes 105, 105
session.findById("wnd[0]").sendVKey 11
strTexto = session.findById("wnd[0]/sbar").Text
Cells(lin_atual, 21) = strTexto
ContinueForLoop:
Next lin_atual
MsgBox ("Documentos contabilizados")
End Sub