我每天都会获得文档列表(将近500个唯一的文档编号),我需要从SAP下载每个文档的附件。我记录了SAP scrip,但无法遍历所有5个文档。需要帮助。
我需要输入凭证编号,公司代码和会计年度。 (全部将是动态的”)如何创建变量并循环以从Excel工作表中为这三个critiera选择值?
Public Sub SAPlogin()
Set WshShell = CreateObject("WScript.Shell")
Set proc = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")
Dim temp
temp = Timer
Do While Timer - temp < 5
Loop
Set SapGui = GetObject("SAPGUI")
Set Appl = SapGui.GetScriptingEngine
Set Connection = Appl.Openconnection("ERP", True)
Set session = Connection.Children(0)
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "XXXX"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "XXXX"
session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"
session.findById("wnd[0]").sendVKey 0
If Not IsObject(Application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Appl = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Application.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 Application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "fb03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = "1"
session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = "20"
session.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = "2019"
session.findById("wnd[0]/usr/txtRF05L-GJAHR").SetFocus
session.findById("wnd[0]/usr/txtRF05L-GJAHR").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").currentCellColumn = "BITM_DESCR"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarButton "%ATTA_EXPORT"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]").sendVKey 12
session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = "2"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").currentCellColumn = "BITM_DESCR"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarButton "%ATTA_EXPORT"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]").sendVKey 12
End Sub
答案 0 :(得分:0)
首先简化您拥有的脚本,然后将其分解为较小的组件。例如,创建一个用于登录的功能,特别是如果您要开始创建一些其他功能的话。
Public Function StartSAPSession() as Variant
' Enter your script here for logging in and starting a new session
'....
' Return the session
Set StartSAPSession = Connection.Children(0)
End Function
我将在全局范围内声明会话对象,以便每次运行宏时都无需登录和打开新会话。请注意,以小写形式使用会话将简化从SAP GUI脚本记录器的复制和粘贴。
Public session as Variant
然后实际上是在“做”你所追求的...
让我们假设您有Table1以及需要查询的3个字段;文件编号,公司代码和会计年度。我们将循环浏览该表中的每一行。我们还将假设每个文档的数量可能会更改。 我们检查会话是否存在(如果不创建会话),填充查询字段,通过上下文菜单查看附件并下载。 注意:我目前无法访问SAP实例,因此没有机会进行测试。它是根据我以前编译的工具改编的。它将需要逐步进行并进行一些测试。我通常还进行了大量测试,以了解可能会收到的错误,然后找到有效处理这些错误的方法。 无论如何,你去...
Public Function ExtractDocuments()
Dim Arr() as Variant
Dim DocNum as String
Dim Company as String
Dim FY as String
Dim AttCnt as Integer
Dim i as Long
Dim j as Long
' When session is Nothing then we need to create a new session
' else assume we can re-use the session
If session Is Nothing Then
Set session = StartSAPSession
End If
' Load the table as an Array, this will be faster
Arr = Range("Table1").ListObject.DataBodyRange
' Cycle through each row of the table (Arr)
For i = 1 to Ubound(Arr, 1)
' Start by loading the row you will enter
DocNum = Arr(i, 1)
Company = Arr(i, 2)
FY = Arr(i, 3)
With session
.findById("wnd[0]").maximize
.StartTransaction "FB03" ' Load the transaction you are after
.findById("wnd[0]/usr/txtRF05L-BELNR").Text = DocNum
.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = Company
.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = FY
.findById("wnd[0]").sendVKey 0 ' Execute transaction
' The query runs and you select context menu and attachments
.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
' How many attachments are there? If 1 or more then save each.
AttCnt = .findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").RowCount
If AttCnt > 0 Then
For j = 0 to AttCnt -1
.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = j
.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarButton "%ATTA_EXPORT"
' Right here you might need to determine the path and name for the file.
' I used to have a version of the following line
.findById("wnd[1]/usr/ctxtDY_PATH").Text = DocNum & "_" & Company & "_" & FY
.findById("wnd[1]/tbar[0]/btn[0]").press
Next j
.findById("wnd[1]/tbar[0]/btn[0]").press ' Exit the Attachments window
End If
End With
Next i
End Function
还有更多机会将其进一步细分为更多的Subs和Function,以使其更具可读性和可重用性。
祝你好运!