我正在编写代码以通过从 Excel 循环中复制一些数据来在 ME23N 中发布/创建 PO。
问题是,当我尝试从另一个 [B] 上的第一个 Notebook[A] 运行脚本时,第二个会给出消息:“无法通过 id 找到控件”。
有没有办法为“findById”代码使用变量,所以无论我在哪里运行它,它都会从 Excel 复制数据并将其粘贴到特定字段?
“On error Go To 0”不起作用,因为即使出现错误,我仍然需要将信息粘贴到该字段。
下面你可以看到我的代码。
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