excel 如何在SAP VBA脚本中创建循环

js5cn81o  于 2022-12-05  发布在  其他
关注(0)|答案(2)|浏览(172)

VBA SAP脚本出现问题。Excel中有数据,我想通过从Excel中复制和粘贴数据来执行事务代码IW41。Excel中有日期、订单数、执行者等所有数据,我想自动执行此操作。我对变量i执行了循环,但出现错误,无法修复。
错误:
运行时错误'619':应用程序定义或对象定义的错误
代码如下所示。
你能给予我一些专业的建议或者帮我修一下吗?
IW41中出现错误的屏幕:

编码:

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
yi0zb3m4

yi0zb3m41#

注意:您需要退出Do循环,并将记录指针i置于循环之外。否则i = 1。要退出循环,我通常使用第一个空白单元格值。这样您就可以设置第一条记录作为开始,并且在大多数情况下i=2,因为大多数工作表都使用第一行作为标题。

i = 1
Do Until Cells(i, 1) = ""
' code  
i = i + 1 
Loop

当我调试的代码使用它在我自己的设备更新在SAP中,我发现我们的SAP没有连接。描述,所以我只是拿起第一个会话这样。
这对我来说很好:

Sub SetEQLocations()
' Script written by Svein Aren Hylland 02.12.2022
' Use of VBScript recording from SAP to work with SAP transaction IE02 - Change Equipment.
' The sub will transfere new Location data found in sheet on all visible rows with filter and headers in first row.
' Code will show progress in first column while it updates each EQ in SAP.
'

Sv = MsgBox("This routine work towards SAP IE02 from row 2 - and will update all EQ locations as shown in this sheet.", vbOKCancel)
If Sv = vbCancel Then Exit Sub

SystemName = "KO3"          '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

'MsgBox Sap_Applic.Children(0).info.SystemName

bSession = False
For iConnectionCounter = 0 To qConnections - 1
    Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
    'MsgBox Connection.Description
    'If Not Connection.Description = "" Then
    qSessions = Connection.Children.Count
        For iSessionCounter = 0 To qSessions - 1
            Set Session = Connection.Children(Int(iSessionCounter))
            'MsgBox Session.info.SystemName
        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
'Stop

Session.findById("wnd[0]").resizeWorkingPane 154, 24, False
Session.findById("wnd[0]/tbar[0]/okcd").Text = "ie02"
Session.findById("wnd[0]").sendVKey 0

i = 2
Do Until Cells(i, 1) = ""
If Cells(i, 1).Rows.Hidden = False Then

    EQ = Cells(i, 1)
    ' Display progress
    Cells(i, 1).Select
    Cells(i, 1).Interior.Color = vbYellow
    
    ' Get data from sheet to be poulated in SAP fields
    CostCenter = Cells(i, 14)    ' Organization/Cost Center
    MainWC = Cells(i, 15)        ' Organization/Main Work Center
    LocWorkCenter = Cells(i, 16) ' Location/Work Center
    LocRoom = Cells(i, 17)       ' Location/Room
    
    Session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").Text = EQ
    Session.findById("wnd[0]/tbar[0]/btn[0]").press
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02").Select
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").Text = LocRoom
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").SetFocus
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").caretPosition = 6
    Session.findById("wnd[0]").sendVKey 0
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").Text = LocWorkCenter
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").SetFocus
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").caretPosition = 6
    Session.findById("wnd[0]").sendVKey 0
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03").Select
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-KOSTL").Text = CostCenter
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").Text = MainWC
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").SetFocus
    Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").caretPosition = 6
    Session.findById("wnd[0]").sendVKey 0
    Session.findById("wnd[0]/tbar[0]/btn[11]").press

End If
   i = i + 1
Loop

Exit Sub

ErrorHandler:
MsgBox "No connection to SAP"
End

End Sub
dfddblmv

dfddblmv2#

"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是光标在文本框中的位置。有时候,如果你想替换文本,就需要这样做。但在这种情况下,肯定不需要。

相关问题