【问题标题】:Printing from SAP via VBA通过 VBA 从 SAP 打印
【发布时间】:2017-12-06 00:51:03
【问题描述】:

我编写了一个脚本,通过 VBA 在 SAP 中完成批量发货。在每个完成的装运结束时,我希望 SAP 打印完成的页面作为确认。

Sub STOMacro()

Dim App, Connection, session As Object
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
Set Connection = App.Children(0)
Set session = Connection.Children(0)


If session Is Nothing Then
  Set session = Connection.Children(Int(ses))
End If

If MsgBox("Are you sure you want to acknowledge these STOs?", vbYesNo, "Complete STOs?") = vbNo Then
             Exit Sub
End If

OrderCounter = Range("A:A").Find("*", Range("A64999"), xlValues, xlWhole, xlByRows, xlPrevious).Row

For i = 1 To OrderCounter

Application.DisplayAlerts = False


STO = Range("A" & i).Value

Application.StatusBar = "Acknowledging STO " & i & " out of " & OrderCounter

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nzvmonitor"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/usr/txtV-LOW").Text = "STOPrint"
session.findById("wnd[1]/usr/txtENAME-LOW").Text = ""
session.findById("wnd[1]/usr/txtV-LOW").CaretPosition = 14
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/usr/ctxtS_DLVRY-LOW").Text = "" & STO & ""
session.findById("wnd[0]/usr/ctxtS_DLVRY-LOW").SetFocus
session.findById("wnd[0]/usr/ctxtS_DLVRY-LOW").CaretPosition = 10
session.findById("wnd[0]").sendVKey 8
session.findById("wnd[0]/usr/cntlZVR_OMONITOR_C1/shellcont/shell").currentCellColumn = ""
session.findById("wnd[0]/usr/cntlZVR_OMONITOR_C1/shellcont/shell").selectedRows = "0"
session.findById("wnd[0]/usr/cntlZVR_OMONITOR_C1/shellcont/shell").pressToolbarButton "RCPT"
session.findById("wnd[0]").sendVKey 3
session.findById("wnd[1]/usr/btnBUTTON_1").press
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[0]/usr/cntlZVR_OMONITOR_C1/shellcont/shell").pressToolbarButton "&PRINT_BACK"
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 13
'This is where the print dialogue pops up but I can't interact with it

Next i
MsgBox ("All STOs acknowledged and printed.")
Application.StatusBar = ""


End Sub

我可以让 SAP 打开打印对话框,但用户仍然必须手动单击“确定”才能实际打印页面。这是一个小小的不便,但我想知道是否可以。 Print dialogue

如果有的话,我可以只使用 SendKeys 让打印对话框得到 {Enter} 发送给它,但我不知道如何让 SAP 挂钩到框以发送该命令。

【问题讨论】:

    标签: vba excel printing sap


    【解决方案1】:

    多年来,我们一直在类似的地方使用以下程序代码:

    . . .
    dim Wshell as Object
    
    set Wshell = CreateObject("WScript.Shell")
    
    on error resume next
    Do 
     bWindowFound = Wshell.AppActivate("Print")
     Application.Wait (Now + TimeValue("0:00:01"))
    Loop Until bWindowFound
    
    bWindowFound = Wshell.AppActivate("Print")
    if (bWindowFound) Then
    Wshell.appActivate "Print"
     Application.Wait (Now + TimeValue("0:00:01"))
     Wshell.sendkeys "{ENTER}"
    end if
    
    bWindowFound = Wshell.AppActivate("Print")
    if (bWindowFound) Then
     Wshell.appActivate "Print"
     Application.Wait (Now + TimeValue("0:00:01"))
     Wshell.sendkeys "{TAB}{ENTER}"
    end if
    
    bWindowFound = Wshell.AppActivate("Print")
    if (bWindowFound) Then
     Wshell.appActivate "Print"
     Application.Wait (Now + TimeValue("0:00:01"))
     Wshell.sendkeys "{TAB}{TAB}{ENTER}"
    end if
    'It could be superfluous under certain circumstances.
    session.findById("wnd[1]").Close
    
    Application.Wait (Now + TimeValue("0:00:01"))
    
    on error goto 0
    . . .
    

    但是,由于您无法知道光标当前所在的位置,因此所有可能性都进行了。但如果你知道的话,你可以遗漏一些东西。

    问候, 脚本人

    【讨论】:

    • 完美运行!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-10-20
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多