如何在SAP VBA脚本中进行循环

问题描述

我在使用VBA SAP脚本时遇到了问题。我在excel中有数据,我想通过从Excel复制和粘贴数据来执行事务代码IW41。我在Excel中拥有所有数据,例如日期,订单数,谁做的数据等等,我想使其自动化。我对变量i进行了循环,但出现错误,无法修复。

错误:

运行时错误'619':应用程序定义或对象定义的错误

下面提供的代码。

您能给我一些专业提示或帮助我解决它吗?

IW41中显示发生错误的位置:

enter image description here

代码:

Sub ConfirmPM_Nots()

    SystemName = "CCP"          'change as needed or use a variable
Transaction = "SESSION_MANAGER"      'change as needed or use a variable

On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
    MsgBox "No connection to SAP"
    End
End If

bSession = False
For iConnectionCounter = 0 To qConnections - 1
    Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
    If Not Connection.Description = "" Then
    qSessions = Connection.Children.Count
        For iSessionCounter = 0 To qSessions - 1
            Set session = Connection.Children(Int(iSessionCounter))
        If session.info.SystemName <> SystemName Then Exit For
            If session.info.Transaction = Transaction Then
                bSession = True
                Exit For
            End If
        Next
    End If
    If bSession Then Exit For
Next

If Not bSession Then
    MsgBox SystemName & " not available or free session not available"
    End
End If

Do
i = 1
session.findById("wnd[0]").resizeWorkingPane 128,37,False
session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41"
session.findById("wnd[0]").sendVKey 0

Order = Cells(i,1)
b = Cells(i,2)
c = Cells(i,3)
d = Cells(i,4)

session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10

session.findById("wnd[0]/tbar[0]/btn[11]").press
   i = i + 1
Loop

Exit Sub

ErrorHandler:
MsgBox "No connection to SAP"
End

End Sub

解决方法

"iw41" (来自session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41")仅在当前屏幕为开始菜单时才有效。确定代码 "/niw41" 将始终有效!因此,此代码可能会起作用(未经测试):

Do
i = 1
' session.findById("wnd[0]").resizeWorkingPane 128,37,False
' iw41 only works in the start menu. OK-Code /niw41 will always work!
session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw41"
session.findById("wnd[0]").sendVKey 0

Order = Cells(i,1)
b = Cells(i,2).value
c = Cells(i,3).value
d = Cells(i,4).value

session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
' session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
' session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10

session.findById("wnd[0]/tbar[0]/btn[11]").press
   i = i + 1
Loop

我还用resizeWorkingPanecaretPositionSetFocus注释了这些行,因为通常不需要这样做。 resizeWorkingPane将调整SAPGUI屏幕的大小,而caretPosition是光标在文本框中的位置。例如,如果要替换文本,有时需要。但是在这种情况下肯定不会。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...