Search code examples
excelvbaprintingsap-gui

Printing from SAP GUI via VBA


I have the script below which completes bulk shipments in SAP GUI via VBA. At the end of each completed shipment, I want SAP to print the completed page as confirmation.

I can get SAP to open the print dialogue box but the user still has to manually click "ok" in order to actually print the pages. This is a minor inconvenience but I was wondering if it could.

Print dialogue

If anything, I'm fine with just using SendKeys to make the print dialogue get {Enter} sent to it but I don't know how to make SAP hook onto the box to send that command.

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

Solution

  • For many years we have been using the following program code in a similar place:

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

    However, since you can not know where the cursor is currently located, all possibilities are carried out. But if you know it, you could leave something out.

    Regards, ScriptMan