我正在尝试编写 VBA 脚本来运行 SAP T 代码 ZN_BSEG

问题描述 投票:0回答:0

以下是需要输入的资料

选择标准“公司代码”- 从单元格 A2 中选择的变量 选择标准“文档编号”- 单击“按字段多选”,然后从 C2 单元格复制数据,直到 C 列中包含数据的最后一个单元格,然后使用从剪贴板上传进行粘贴 文件名“布局”- 使用 /1130020N 字段名称“最大命中数”- 将此字段留空 现在执行并将文件导出到“C:\Users itemit\Desktop\SDM FolderK0020\ZN_BSEG.xlsx"

我收到公司代码“session.findById(”wnd[0]/usr/ctxtS_BUKRS-LOW”).Text = companyCode”的错误代码“619”

下面是完整代码

选项显式 公共 SapGuiAuto,WScript 公共 objGui 作为 GuiApplication 公共 objConn 作为 GuiConnection 公开会议作为 GuiSession

子运行SAPTransaction() 昏暗的公司代码作为字符串 将文档编号调暗为字符串

' Get the company code from Sheet1, Cell A2 in the same workbook
companyCode = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
    
' Get the document numbers from Sheet1, starting from Cell C2 to the last cell with data in Column C
documentNumbers = Join(Application.Transpose(ThisWorkbook.Sheets("Sheet1").Range("C2:C" & ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row).Value), ",")

' Connect to SAP
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)

' Maximize the SAP window
session.findById("wnd[0]").Maximize

' Run transaction ZN_BSEG
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nZN_BSEG"
session.findById("wnd[0]").sendVKey 0

' Wait for the SAP session to be ready
Do While session.ActiveWindow.Name = ""
    Application.Wait Now + TimeValue("0:00:01")
Loop

' Enter the company code
session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").Text = companyCode

' Click on "Multiple Selection by Field" for document numbers
session.findById("wnd[0]/usr/txtS_BELNR-LOW").SetFocus
session.findById("wnd[0]/usr/txtS_BELNR-LOW").caretPosition = 10
session.findById("wnd[0]").sendVKey 2

' Paste document numbers from clipboard
session.findById("wnd[1]/tbar[0]/btn[24]").press
session.findById("wnd[1]/tbar[0]/btn[30]").press

' Wait for the SAP session to be ready
Do While session.ActiveWindow.Name = ""
    Application.Wait Now + TimeValue("0:00:01")
Loop

' Choose layout /1130020N
session.findById("wnd[0]/usr/ctxtS_LAYOUT").Text = "/1130020N"

' Leave Maximum no. of hits blank

' Execute the transaction
session.findById("wnd[0]").sendVKey 8

' Wait for the SAP session to be ready
Do While session.ActiveWindow.Name = ""
    Application.Wait Now + TimeValue("0:00:01")
Loop

' Export the file to the specified location
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").PressToolbarButton "&MB_VARIANT"
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").selectContextMenuItem "&LOAD"
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").CurrentCellColumn = "TEXT"
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").ClickCurrentCell
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").setCurrentCell 1, "BSCHL"
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").selectContextMenuItem "&XXL"

' Save the file
session.findById("wnd[1]/tbar[0]/btn[11]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\nitemit\Desktop\SDM Folder\1130020"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "ZN_BSEG.xlsx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
session.findById("wnd[1]/tbar[0]/btn[0]").press

' Close SAP
session.findById("wnd[0]/tbar[0]/btn[15]").press

结束子

excel vba sap
© www.soinside.com 2019 - 2024. All rights reserved.