4

截取活动窗口的屏幕截图。

Set Wshshell=CreateObject("Word.Basic")
WshShell.sendkeys"%{prtsc}"
WScript.Sleep 1500

运行 Mspaint 并粘贴。

set Wshshell = WScript.CreateObject("WScript.Shell")
Wshshell.Run "mspaint"
WScript.Sleep 500

WshShell.AppActivate "Paint"
WScript.Sleep 500

WshShell.sendkeys "^(v)"
WScript.Sleep 1500

在这里,对活动窗口进行截图操作很好。而且,它以mspaint开头,但内容没有粘贴到paint文件中。

4

2 回答 2

5

您 .Sendkeys 的 ^V 参数是错误的,它应该是:

WshShell.sendkeys "^v"

.AppActivate 之后的 .Sleep 似乎很关键;在我增加睡眠时间之前,我无法让它“工作”:

WshShell.AppActivate "Paint"
WScript.Sleep 5000

您的问题证明 .Sendkeys 不可靠。看这里,特别是 Moby Disk 的贴子想想其他的策略。

于 2013-05-05T11:29:12.117 回答
0

如果您想实现“PrintScreen 的 Save-as-JPG”之类的东西,这是我的代码:

' ----------------------------------------------------------------------
' Clipboard to JPG    ...using Word.Basic and Excel
' ----------------------------------------------------------------------
  Dim DosBasic : Set DosBasic = CreateObject("Word.Basic")
  Dim XLS      : Set XLS      = CreateObject("Excel.Application")
  Dim T0       : T0           = Now


  Call GetScreenshot
  Call Ding
  Call MakeFolderIfNotExist(ScreenshotFolder & "\" & CurrDate)
  Call StoreClipboard(CurrDate & "\" & CurrTime & ".jpg")

  XLS.Application.Quit



' ----------------------------------------------------------------------
  Sub MakeFolderIfNotExist(ByVal FolderName)
' ----------------------------------------------------------------------
  Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
  if not FSO.FolderExists(FolderName) then FSO.CreateFolder(FolderName)
  End Sub



' Uses less known Word.Basic to correctly send (Alt+)PrintScreen.
' Unfortunately, the Word.Basic takes SEVERAL seconds to load
' ----------------------------------------------------------------------
  Sub GetScreenshot
' ----------------------------------------------------------------------
  'Dim DosBasic : Set DosBasic = CreateObject("Word.Basic")
  'DosBasic.SendKeys "{1068}"           ' = Printscreen     = entire screen
  DosBasic.SendKeys "%{prtsc}"        ' = Alt+PrintScreen = only active window
  End Sub



' Uses Excel and its mighty Chart object, to create Exportable JPG image
' ----------------------------------------------------------------------
  Sub StoreClipboard(ByVal Filename)
' ----------------------------------------------------------------------
  Const xlLandscape = 2  ' Landscape page
  Const xlPortrait  = 1  ' Portrait page

  'Dim XLS   : Set XLS = CreateObject("Excel.Application")
  Dim Sheet : Set Sheet = XLS.Workbooks.Add
  Dim Chart : Set Chart = XLS.Charts.Add

  Const ScreenshotFolder = "C:\Temp\Screenshots"
  Call MakeFolderIfNotExist(ScreenshotFolder)

  XLS.Visible = False
  XLS.ActiveSheet.PageSetup.Orientation = xlLandscape
  XLS.ActiveWindow.Zoom = 100
  Chart.Paste
  Chart.Export ScreenshotFolder & "\" & Filename
  XLS.ActiveWorkbook.Saved = True
  XLS.ActiveWorkbook.Close   False
  'XLS.Application.Quit
  End Sub



' ----------------------------------------------------------------------
  Function CurrDate
' ----------------------------------------------------------------------
  'Dim T0 : T0 = Now
  CurrDate = Year(T0) & "-" & Right("0"&Month(T0),2) & "-" & Right("0"&Day(T0),2)
  End Function



' ----------------------------------------------------------------------
  Function CurrTime
' ----------------------------------------------------------------------
  'Dim T0 : T0 = Now
  CurrTime = Right("0"&Hour(T0),2) & "." & Right("0"&Minute(T0),2) & "." & Right("0"&Second(T0),2)
  End Function



' Play selected sound to indicate 'finish successfully'
' ----------------------------------------------------------------------
  Sub Ding
' ----------------------------------------------------------------------
  Const wavFile = "C:\Windows\media\Windows Background.wav"
  Dim oVoice        : Set oVoice        = CreateObject("SAPI.SpVoice")
  Dim oSpFileStream : Set oSpFileStream = CreateObject("SAPI.SpFileStream")
  oSpFileStream.Open wavFile
  oVoice.SpeakStream oSpFileStream
  oSpFileStream.Close
  End Sub

效果很好。只是有点慢 - 创建“Word.Basic”会导致 cca 5 秒延迟。不知道为什么。之后,Excel 工作正常。

例如,您可以让它在 Ctrl+F12 或类似的热键上运行(通过创建快捷方式),然后在任何地方都可以使用。

于 2019-11-06T23:05:03.083 回答