martin
martin

Reputation: 11

Printscreen from SAP GUI by VBA

I want to make print screen by VBA, but unfortunately I got only screens from VBA.

How to do it in a proper way?

Please find my script below:

Sub mb()
Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
  Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
  Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
  Set session = SAPCon.Children(0)
  
session.findbyId("wnd[0]").maximize
session.findbyId("wnd[0]/tbar[0]/okcd").Text = "/n"
session.findbyId("wnd[0]").sendVKey 0
session.findbyId("wnd[0]/tbar[0]/okcd").Text = "fs10n"
session.findbyId("wnd[0]").sendVKey 0
session.findbyId("wnd[0]/usr/ctxtSO_SAKNR-LOW").Text = Cells(5, 2)
session.findbyId("wnd[0]/usr/ctxtSO_BUKRS-LOW").Text = Cells(5, 3)
session.findbyId("wnd[0]/usr/txtGP_GJAHR").Text = Cells(5, 4)
session.findbyId("wnd[0]/usr/txtGP_GJAHR").SetFocus
session.findbyId("wnd[0]/usr/txtGP_GJAHR").caretPosition = 4
session.findbyId("wnd[0]/tbar[1]/btn[8]").press
'session.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
'session.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").selectContextMenuItem "&HTML"
session.findbyId("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").setCurrentCell Cells(5, 5), "BALANCE_CUM"

AppActivate
Application.SendKeys "(%{1068})"
DoEvents
Cells(1, 1).Select
ActiveSheet.Paste

Upvotes: 1

Views: 5466

Answers (1)

Nick
Nick

Reputation: 3845

Sendkeys is very unreliable and has frustrated me in the past. This is what i use:

At the top of a vba module place these lines

Option Explicit

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12

Then paste this sub and call it:

Sub PrintScreen()
    keybd_event VK_SNAPSHOT, 1, 0, 0
End Sub

cheers

Upvotes: 0

Related Questions