SAP脚本:遍历参考

时间:2018-08-16 06:21:52

标签: excel-vba sapscript

我当前正在创建一个SAP脚本来自动执行任务。基本上,此任务与清算收款/付款有关。

SAP系统:Q4N(P4S的测试系统) 交易:银行对帐单后处理

我在另一本工作簿中有此数据:

enter image description here

现在,我需要按以下顺序在SAP中输入此详细信息:

  1. 日期
  2. 总金额
  3. 参考

在第1项和第2项中输入详细信息之后,我遇到的麻烦是“参考”,因为如果没有适当的订单项,我需要在第一个文本字段中一个一个地输入,以捕获错误。

enter image description here

输入/处理所有参考后,将继续处理另一个库。由于可能会有其他库要处理。一个例子是:

enter image description here

我下面有一个工作代码。但是我的问题是,它只是基于第一个屏幕截图在“参考列”中循环,并且不会停止。

Dim i As Long, x As Long, RefLastRow As Long
Dim stmtDate$, TotalAmount$, CurrencyFormat$, SAPCurrency$, TempCurrency$, sText
Dim RefCount As Long

Dim Ret
Dim FilePath As String
FilePath = wMain.Range("Path")
Dim fso As New FileSystemObject
Dim fileName As String
Dim w As Workbook

fileName = fso.GetFileName(FilePath)
Ret = IsWorkBookOpen(FilePath)

If Ret = False Then

MsgBox "Please prepare the template file first", vbCritical, "Warning!"

Else

Set w = Workbooks(fileName)

With session

RefLastRow = w.Worksheets(1).Cells(Worksheets(1).Rows.Count, "H").End(xlUp).Row


For i = 2 To RefLastRow


stmtDate = w.Worksheets(1).Cells(i, 2).Value
TotalAmount = w.Worksheets(1).Cells(i, 3).Value
TempCurrency = w.Worksheets(1).Cells(i, 9).Value


        .findById("wnd[0]").Maximize
        .findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").DoubleClickNode "F00002" 'Double click Nachbehand in favorites

        .findById("wnd[0]/usr/ctxtFEBEBA-BUKRS").Text = "0156" 'company code default
        .findById("wnd[0]/usr/ctxt/BAY0/AB_EBS_GRA-AZDATL").Text = stmtDate 'insert date
        .findById("wnd[0]/usr/txt/BAY0/AB_EBS_GRA-KWBTRL").Text = TotalAmount 'insert amount

        .findById("wnd[0]/usr/txt/BAY0/AB_EBS_GRA-KWBTRL").SetFocus 'Show list
        .findById("wnd[0]/usr/txt/BAY0/AB_EBS_GRA-KWBTRL").caretPosition = 10 'show list
        .findById("wnd[0]/tbar[1]/btn[8]").press 'Showlist

        .findById("wnd[0]/usr/lbl[9,2]").SetFocus 'choose bank
        .findById("wnd[0]/usr/lbl[9,2]").caretPosition = 6 'bank position
        .findById("wnd[0]").sendVKey 2 'click bank

        .findById("wnd[0]/usr/lbl[8,5]").SetFocus 'result of bank
        .findById("wnd[0]/usr/lbl[8,5]").caretPosition = 8 'result of bank position
        .findById("wnd[0]").sendVKey 2 'doubleclick bank

        .findById("wnd[0]/tbar[1]/btn[13]").press 'posting mode
        .findById("wnd[1]/usr/lbl[3,3]").SetFocus 'Display All screens
        .findById("wnd[1]/usr/lbl[3,3]").caretPosition = 11 'Display all screens position
        .findById("wnd[1]").sendVKey 2 'double click display all screens position

        .findById("wnd[0]/tbar[1]/btn[6]").press 'Press post SA button

        'Currency check if same currency in the file, then value is default.
        'If value is different, then copy the currency in the template and paste in this field

        SAPCurrency = .findById("wnd[0]/usr/ctxtBKPF-WAERS").Text

            If SAPCurrency <> TempCurrency Then

                .findById("wnd[0]/usr/ctxtBKPF-WAERS").Text = TempCurrency

            End If

        .findById("wnd[0]/usr/txtRF05A-AUGTX").SetFocus
        .findById("wnd[0]/usr/txtRF05A-AUGTX").caretPosition = 4
        .findById("wnd[0]/tbar[1]/btn[6]").press

         sText = .findById("wnd[0]/sbar").Text

        If sText = "Posting takes place in previous fiscal year" Then

            .findById("wnd[0]").sendVKey 0

        End If

        .findById("wnd[0]/tbar[0]/okcd").Text = "/06"
        .findById("wnd[0]").sendVKey 0
        .findById("wnd[0]/tbar[0]/okcd").Text = "/00"
        .findById("wnd[1]").sendVKey 0
        .findById("wnd[0]/usr/sub:SAPMF05A:0710/radRF05A-XPOS1[5,0]").Select
        .findById("wnd[0]/tbar[0]/okcd").Text = "/11"
        .findById("wnd[0]/usr/sub:SAPMF05A:0710/radRF05A-XPOS1[5,0]").SetFocus
        .findById("wnd[0]").sendVKey 0

'****This is where I'm starting to have a trouble******  

        RefCount = 2

        Do While w.Worksheets(1).Cells(i, 1).Value <> vbNullString
        RefCount = RefCount + 1

        'start entering reference number from the template to this field
        .findById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").Text = w.Worksheets(1).Cells(RefCount, 8).Value 'First field in reference
        .findById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").caretPosition = 10 'Position 'position of reference in selection
        .findById("wnd[0]").sendVKey 0 'double click selected reference

        'Check Status Bar of SAP if there is an error for each reference number entered

        sText = .findById("wnd[0]/sbar").Text
        If sText = "No appropriate line item is contained in this document" Then

            MsgBox "Please check line" & " " & w.Worksheets(1).Cells(RefCount, 8).Row & " " & "in column 'Reference'" _
            & vbNewLine & vbNewLine & "Reason: " & sText, vbCritical, "Performance Assistant"
            End

        End If

        Loop


Next i

MsgBox "Process Completed!", vbOKOnly


    End With
'End of sap script code

End If

0 个答案:

没有答案