sap脚本运行_SAP录制的VBS脚本在VBA中运行

该脚本展示了如何在VBA中调用SAP GUI,通过CK11N交易码进行批量操作。它遍历Excel工作表中的数据,填充SAP字段并执行相应步骤,最后更新行颜色以标识已处理的记录。
摘要由CSDN通过智能技术生成

Option Explicit

Public SapGuiAuto As Object

Public Applicat As Object

Public Connection As Object

Public Session As Object

Public StaBar As Object

Public SubTbar As Object

Public subWindow As Object

Public arr     '数据区域

Function Attach() As Boolean              '是否调用SAPGUI

On Error Resume Next

Set SapGuiAuto = GetObject("SAPGUI")

On Error GoTo 0

If SapGuiAuto Is Nothing Then

Attach = False

Exit Function

Else

Set Applicat = SapGuiAuto.GetScriptingEngine

On Error GoTo 0

End If

If Applicat Is Nothing Then

MsgBox "Scripting Disabled"

Attach = False

Exit Function

End If

If Applicat.Children.Count = 0 Then

Attach = False

Exit Function

Else

Set Connection = Applicat.Children(0)

On Error GoTo 0

End If

Set Session = Connection.Children(0)

On Error GoTo 0

If Session.ActiveWindow.text = "SAP" Then

Attach = False

Exit Function

End If

Attach = True

End Function

'==========================================================================

Sub CK11N()

Dim i&, j&

Dim arr

Dim t

t = Timer

arr = Sheet1.UsedRange

If Attach Then

'Stop

For i = 2 To UBound(arr)

On Error GoTo errLine

Session.findById("wnd[0]/tbar[0]/okcd").text = "ck11n"

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

Call WaitMoment(2)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/subKOPF:SAPLCKDI:4620/ctxtCKI64A-MATNR").text = arr(i, 1)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/subKOPF:SAPLCKDI:4620/ctxtCKI64A-WERKS").text = arr(i, 2)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpALLG/ssubALLGEMEIN:SAPLCKDI:4612/ctxtCKI64A-KLVAR").text = arr(i, 3)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpALLG/ssubALLGEMEIN:SAPLCKDI:4612/ctxtCKI64A-TVERS").text = arr(i, 4)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpALLG/ssubALLGEMEIN:SAPLCKDI:4612/ctxtCKI64A-UEBID").text = arr(i, 5)

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

Call WaitMoment(2)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpTERM/ssubTERM:SAPLCKDI:4614/ctxtCKI64A-KADAT").text = arr(i, 6)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpTERM/ssubTERM:SAPLCKDI:4614/ctxtCKI64A-BIDAT").text = arr(i, 7)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpTERM/ssubTERM:SAPLCKDI:4614/ctxtCKI64A-ALDAT").text = arr(i, 8)

Session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpTERM/ssubTERM:SAPLCKDI:4614/ctxtCKI64A-BWDAT").text = arr(i, 8)

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

Call WaitMoment(2)

Session.findById("wnd[0]/tbar[0]/btn[11]").press

Session.findById("wnd[1]/tbar[0]/btn[0]").press

Call WaitMoment(2)

Session.findById("wnd[0]/tbar[0]/btn[3]").press

Session.findById("wnd[0]/tbar[0]/btn[3]").press

Sheet1.Rows(i).Interior.ColorIndex = 33

Next i

End If

MsgBox ("Done " & (Timer - t))

Exit Sub

errLine:

MsgBox ("ERROR " & i)

Stop

End Sub

Private Sub WaitMoment(rMoment!)

Dim rT!

rT = Timer

Do While Timer - rT < rMoment

DoEvents

Loop

End Sub

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值