VBA调用SAP GUI

原创
2017/10/31 14:04
阅读数 9.5K

上半部分为判断SAP GUI是否可用

下半部分是实际运用T-Code CK11N 

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

 

展开阅读全文
SAP
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部