1

我想用 VBA 制作打印屏幕,但不幸的是我只有 VBA 的屏幕。

如何以正确的方式做到这一点?

请在下面找到我的脚本:

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
4

1 回答 1

0

Sendkeys 非常不可靠,过去让我很沮丧。这就是我使用的:

在 vba 模块的顶部放置这些行

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

然后粘贴这个子并调用它:

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

干杯

于 2017-02-03T10:09:10.583 回答