从SAP打印通过VBA

我有一个脚本放在一起,通过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打开打印对话框,但用户仍然需要手动点击“确定”才能打印页面。 这是一个小小的不便,但我想知道是否可以。 打印对话框

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

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

 . . . 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 . . . 

但是,由于您无法知道光标当前所在的位置,因此将执行所有可能的操作。 但是如果你知道的话,你可以留下一些东西。

问候,ScriptMan