-1

我在 VBA 中为我们的 IBM iSeries / AS400 / 绿屏环境创建了一个宏,它在采购订单中查找项目编号,然后更改它们的价格并退出采购订单。在宏中是一个循环,用于解析采购订单的每一行,如果在第一页上找不到编号,则向下翻页到下一页。这是循环的代码:

'ItemNum and Price are entered by the user within Excel
Do Until IBMItemNum = ItemNum
    If NumRow = 11 Then
        'There are ten lines within purchase orders, after which one must page down
        'to see the rest of the purchase order. This is where the problem occurs
        autECLSession.autECLPS.SendKeys "[pagedn]"
        NumRow = 1
    End If
    IBMItemNum = autECLSession.autECLPS.GetText(NumRow, 2, 5)
    If IBMItemNum = ItemNum Then
        autECLSession.autECLPS.SetCursorPos NumRow, 66
        autECLSession.autECLPS.SendKeys Price
        autECLSession.autECLPS.SendKeys "[field+]"
        autECLSession.autECLPS.SendKeys "[enter]"
        'After the item has been found and the price entered, the macro exits the PO
        'Entering PageDown after the PO has been exited crashes the AS400 session
        Exit Do
    Else
        'Check the next row
        NumRow = NumRow + 1
    End If
Loop

当且仅当我从循环中删除 PageDown SendKeys 命令时,此宏才能连续成功读取采购订单第一页上的每个数字。但是,即使 ItemNum 是 PO 上的第一个 IBMItemNum,宏也不会退出循环并会向下翻页(这也意味着循环已重复 10 次以上)。我的代码是否缺少退出条件的内容?

4

1 回答 1

0

之后您将需要延迟autECLSession.autECLPS.SendKeys "[enter]"。这将使屏幕有时间更新。

Sleep API 非常理想Sleep 250,它将停止您的代码 500 毫秒(1/2 秒)。

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

最简单的解决方法是使用较大的延迟。或者,您可以使用较小的延迟并使用GetText来查看屏幕是否已更新。

'ItemNum and Price are entered by the user within Excel
Do
    If NumRow = 11 Then
        'There are ten lines within purchase orders, after which one must page down
        'to see the rest of the purchase order. This is where the problem occurs
        autECLSession.autECLPS.SendKeys "[pagedn]"
        NumRow = 1
    End If
    IBMItemNum = autECLSession.autECLPS.GetText(NumRow, 2, 5)
    If IBMItemNum = ItemNum Then
        autECLSession.autECLPS.SetCursorPos NumRow, 66
        autECLSession.autECLPS.SendKeys Price
        autECLSession.autECLPS.SendKeys "[field+]"
        autECLSession.autECLPS.SendKeys "[enter]"
        Sleep 500
        'After the item has been found and the price entered, the macro exits the PO
        'Entering PageDown after the PO has been exited crashes the AS400 session
        Exit Do 'Not needed
    Else
        'Check the next row
        NumRow = NumRow + 1
    End If
    DoEvents

Loop Until IBMItemNum = ItemNum
于 2018-05-23T19:05:02.707 回答