繁体   English   中英

通过VBA从SAP打印

[英]Printing from SAP via 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM