Excel VBA 不复制字符串

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

我正在 SAP 中运行一些简单、易于自动化的事务,使用 Excel 宏来驱动它。它几乎只是将销售订单编号从很长的列表复制到特定字段中,单击一些按钮,然后将生成的合同编号复制回 Excel 文件中的列表中。

每当遇到错误或花费特别长的时间时,当它开始下一个循环时,它不会从 Excel 文件中复制销售订单号,除非我输入停止线 - 它只会在我输入停止线时复制它有点复杂,我宁愿启动它并让它运行,而不需要照顾它。

Sub ContractMacro()

Dim MyApplication As SAPFEWSELib.GuiApplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim session As SAPFEWSELib.GuiSession
Dim SapGuiAuto As Object
Dim wb As Workbook
Dim currentso As Range
Dim contractrng As Range
Dim cell As Range
Dim x As Integer

Set SapGuiAuto = GetObject("SAPGUI").getscriptingengine
Set objGui = SapGuiAuto.getscriptingengine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Set wb = ThisWorkbook

Set contractrng = Range("A2:A" & WorksheetFunction.CountA(Range("A:A")))
Application.DisplayAlerts = False

session.findById("wnd[0]/usr/ctxtS_FKDAT-LOW").Text = "01.01." & Year(Date) - 1
For Each cell In contractrng
'    If Now >= Range("C4") And Now >= Range("C2") Then
'        Exit Sub
'    Else
'    End If
2
    session.findById("wnd[0]/usr/ctxtS_FKDAT-LOW").Text = "01.01." & Year(Date) - 1
    x = WorksheetFunction.CountA(Range("B:B")) + 1
    Set currentso = Cells(x, 1)
    If currentso = "" Then
        GoTo 4
    End If
    session.findById("wnd[0]/usr/ctxtS_AUBEL-LOW").Text = currentso
'************* line above does not carry over currentso************************ 
    If session.findById("wnd[0]/usr/ctxtS_AUBEL-LOW").Text = "" Then
    session.findById("wnd[0]/usr/ctxtS_AUBEL-LOW").Text = currentso
    Stop
    session.findById("wnd[0]/usr/ctxtS_AUBEL-LOW").Text = currentso
    Stop
    Else
    End If
'************** if statement above was added to manually make sure it's copied over***********
    On Error GoTo 3
    session.findById("wnd[0]/tbar[1]/btn[8]").press
    Select Case session.findById("wnd[0]/usr/shell").GetCellValue(0, "STATUS")
        Case Is = "A"
            session.findById("wnd[0]/usr/shell").setCurrentCell -1, ""
            session.findById("wnd[0]/usr/shell").SelectAll
            session.findById("wnd[0]/usr/shell").PressToolbarButton "ZIH20"
            session.findById("wnd[0]/tbar[1]/btn[8]").press
            session.findById("wnd[0]/usr/shell").setCurrentCell -1, ""
            session.findById("wnd[0]/usr/shell").SelectAll
            session.findById("wnd[0]/usr/shell").PressToolbarButton "VFLG"
            Cells(currentso.Row, 2) = session.findById("wnd[0]/usr/shell").GetCellValue(0, "SDAUFNR")
            session.findById("wnd[0]/tbar[0]/btn[3]").press
            session.findById("wnd[0]/tbar[0]/btn[3]").press
            session.findById("wnd[0]/tbar[0]/btn[3]").press
        Case Else
            Cells(currentso.Row, 2) = session.findById("wnd[0]/usr/shell").GetCellValue(0, "STATUS")
            session.findById("wnd[0]/tbar[0]/btn[3]").press
    End Select
    wb.Save
Next cell
4
Application.DisplayAlerts = True

Exit Sub

3
On Error GoTo -1
On Error Resume Next
Cells(currentso.Row, 2) = Trim(session.findById("wnd[1]/usr/txtMESSTXT1").Text)
session.findById("wnd[0]/tbar[0]/okcd").Text = "/N ZI_EW2_CONTROL"
session.findById("wnd[0]").SendVKey 0
GoTo 2
End Sub

有时交易会给出除合约号之外的其他结果,这就是为什么有一些额外的东西,但重要的部分是用星号指出的循环开始的部分。我在循环中做错了什么吗?几乎感觉它只是想太快地单击按钮,所以我尝试添加等待和睡眠行,但它仍然不会填写该字段 - 只需直接穿过该行即可。

值得一提的是,只要在停止时按 F5 就足以让代码复制数据。如果没有停止,它不会复制它,但是当它停止时,它会复制它。非常感谢您的帮助,因为我有 2000 个这样的问题需要完成,我真的很想让它运行几天

我尝试过等待和睡眠线路,认为只是需要更多时间。我什至尝试放置相同的两行(等待,复制,等待,复制)只是为了确保,但无济于事。它只是按下回车键,而没有输入字段中的任何内容,这导致它挂起,试图抓取当年的所有内容。

excel vba copy sap-gui sapscript
1个回答
0
投票

找到解决方案了!我不只是引用 currentso 范围,而是将其更改为 currentso.text

如果其他人遇到数据未复制到 SAP 中的情况,请尝试这样做!

© www.soinside.com 2019 - 2024. All rights reserved.