Vbscript打印屏幕到MS画

时间:2013-05-05 11:10:41

标签: windows vbscript printscreen

拍摄活动窗口的屏幕截图。

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文件中。

2 个答案:

答案 0 :(得分:4)

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

WshShell.sendkeys "^v"

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

WshShell.AppActivate "Paint"
WScript.Sleep 5000

你的问题证明了这一点.Sendkeys不可靠。看here, especially the posting of Moby Disk来考虑其他策略。

答案 1 :(得分:0)

如果您想实现“ PrintScreen的另存为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之类的热键上运行或通过类似方式(通过创建快捷方式)运行,然后在任何地方都可以使用。