Vba 如何执行Prnt屏幕并将其保存到特定文件夹中

Vba 如何执行Prnt屏幕并将其保存到特定文件夹中,vba,internet-explorer,automation,Vba,Internet Explorer,Automation,我找到了一个代码,可以在不使用Sendkeys方法的情况下执行Prnt屏幕: 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 VK_SNAPSHOT = &H2C Sub PrintScreen

我找到了一个代码,可以在不使用Sendkeys方法的情况下执行Prnt屏幕:

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 VK_SNAPSHOT = &H2C

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

我还没有尝试过它是否能正常工作(如果不能,我将使用Sendkeys),但我想知道是否有办法将其保存为.pdf/.jpg(无所谓)到一个特定的文件夹中。所有的打印屏幕都是关于Internet Explorer页面的。

您的代码只模拟“按下”PrtScrn键,而不是“释放”它。在此处添加第二行,如下所示:

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 VK_SNAPSHOT = &H2C
Private Const KEYEVENTF_KEYUP = 2

Sub PrintScreen()
    keybd_event VK_SNAPSHOT, 1, 0, 0 'Key Down
    keybd_event VK_SNAPSHOT, 1, KEYEVENTF_KEYUP , 0 'Key Up
End Sub
然后,您可以将屏幕截图粘贴到工作表并将其导出为PDF

Sub SaveAsPDF()
    Const FILE_PATH as String = "C:\Temp\"
    PrintScreen 'Take a screenshot
    With Sheet1
        .Paste 'Paste it to Sheet1
        .ExportAsFixedFormat xlTypePDF, FILE_PATH & "Screenshot File.pdf" 'Export Sheet1 to PDF
    End With
End Sub

您拥有的代码仅模拟“按下”PrtScrn键,而不是“释放”它。在此处添加第二行,如下所示:

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 VK_SNAPSHOT = &H2C
Private Const KEYEVENTF_KEYUP = 2

Sub PrintScreen()
    keybd_event VK_SNAPSHOT, 1, 0, 0 'Key Down
    keybd_event VK_SNAPSHOT, 1, KEYEVENTF_KEYUP , 0 'Key Up
End Sub
然后,您可以将屏幕截图粘贴到工作表并将其导出为PDF

Sub SaveAsPDF()
    Const FILE_PATH as String = "C:\Temp\"
    PrintScreen 'Take a screenshot
    With Sheet1
        .Paste 'Paste it to Sheet1
        .ExportAsFixedFormat xlTypePDF, FILE_PATH & "Screenshot File.pdf" 'Export Sheet1 to PDF
    End With
End Sub

我在另一个线程中发现了下面的代码,该线程正在按照您的要求运行

我对它进行了测试,它正在拍摄快照并将其保存到特定文件夹中

Option Explicit
'requires references: "Microsoft HTML Object Library" & "Microsoft Internet Controls"

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

Sub getSS()
  Const url = "stackoverflow.com" 'page to get screenshot of (http is added below)
  Const fName = "D:\thumb_" & url & ".png" 'output filename (can be png/jpg/bmp/gif)
  Const imgScale = 0.25 'scale to 25% (to create thumbnail)

  Dim ie As InternetExplorer, ws As Worksheet, sz As Long
  Dim img As Picture, oCht As ChartObject
  Set ws = ThisWorkbook.Sheets("Sheet1")
  Set ie = GetIE()
  With ie
    .navigate "http://" & url
    Do: DoEvents: Loop While .busy Or .readyState <> 4 'wait for page load
    ShowWindow Application.hwnd, 5 'activate IE window
    Call keybd_event(44, 0, 0, 0) '44="VK_SNAPSHOT"
    Pause (0.25) 'pause so clipboard catches up
    With ws
      ShowWindow Application.hwnd, 5 'back to Excel
      .Activate
      .Paste
      Set img = Selection
      With img
        Set oCht = ws.ChartObjects.Add(.Left, .Top, .Left + .Width, .Top + .Height)
        oCht.Width = .Width * imgScale 'scale obj to picture size
        oCht.Height = .Height * imgScale
        oCht.Activate
        ActiveChart.Paste
        ActiveChart.Export fName, Mid(fName, InStrRev(fName, ".") + 1)
        oCht.Delete
        .Delete
      End With
      .Activate
    End With
    .FullScreen = False
    .Quit
  End With
  If Dir(fName) = "" Then Stop 'Something went wrong (file not created)
  sz = FileLen(fName)
  If sz = 0 Then Stop 'Something went wrong! (invalid filename maybe?)
  Debug.Print "Created '" & fName & "' from '" & url & "' (" & sz & " bytes)": Beep
End Sub

Sub Pause(sec As Single)
  Dim t As Single: t = Timer
  Do: DoEvents: Loop Until Timer > t + sec
End Sub

Function GetIE() As Object
'requires references: "Microsoft HTML Object Library" & "Microsoft Internet Controls"
'return an object for the open Internet Explorer window, or create new one
  For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
    If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
  Next GetIE
  If GetIE Is Nothing Then Set GetIE=CreateObject("InternetExplorer.Application") 'Create
  GetIE.Visible = True 'Make IE visible
  GetIE.FullScreen = True
End Function
选项显式
'需要引用:“Microsoft HTML对象库”和“Microsoft Internet控件”
Private Declare Sub-keybd_事件库“user32”(ByVal bVk作为字节_
ByVal bScan为字节,ByVal dwFlags为长,ByVal dwExtraInfo为长)
私有声明函数ShowWindow Lib“user32”_
(ByVal hwnd尽可能长,ByVal nCmdShow尽可能长)尽可能长
子getSS()
Const url=“stackoverflow.com”页面以获取屏幕截图(下面添加了http)
Const fName=“D:\thumb\ux”&url&“.png”输出文件名(可以是png/jpg/bmp/gif)
常量imgScale=0.25'缩放至25%(用于创建缩略图)
Dim ie作为InternetExplorer,ws作为工作表,sz作为长
将img调暗为图片,将oCht调暗为图表对象
设置ws=ThisWorkbook.Sheets(“Sheet1”)
设置ie=GetIE()
与ie
。导航“http://”和url
Do:DoEvents:While.busy或.readyState 4'等待页面加载时循环
ShowWindow Application.hwnd,5'激活IE窗口
调用keybd_事件(44,0,0,0)'44=“VK_快照”
暂停(0.25)'暂停以便剪贴板跟上
与ws
ShowWindow Application.hwnd,5'返回Excel
.激活
粘贴
设置img=选择
与img
设置oCht=ws.ChartObjects.Add(.Left、.Top、.Left+.Width、.Top+.Height)
oCht.Width=.Width*imgScale“缩放对象到图片大小
oCht.Height=.Height*imgScale
哦,激活
活动图表。粘贴
ActiveChart.Export fName,Mid(fName,InStrRev(fName,“.”+1)
oCht.删除
.删除
以
.激活
以
.FullScreen=False
退出
以
如果Dir(fName)=“Then Stop”出错(文件未创建)
sz=FileLen(fName)
如果sz=0,则停止“出现问题!”!(可能是无效的文件名?)
Debug.Print“Created'&fName&“from'&url&'(&sz&“bytes”):嘟嘟声
端接头
子暂停(秒为单个)
将t调为单个:t=计时器
Do:DoEvents:循环直到计时器>t+秒
端接头
函数GetIE()作为对象
'需要引用:“Microsoft HTML对象库”和“Microsoft Internet控件”
'为打开的Internet Explorer窗口返回对象,或创建新对象
对于CreateObject(“Shell.Application”).Windows()循环中的每个GetIE
如果(非GetIE为Nothing)和GetIE.Name=“Internet Explorer”,则退出“已找到”!
下一个盖蒂
如果GetIE为Nothing,则设置GetIE=CreateObject(“InternetExplorer.Application”)'创建
GetIE.Visible=True“使IE可见”
GetIE.FullScreen=True
端函数
参考:


此外,您可以根据自己的要求修改代码。

我在另一个线程中发现了下面的代码,该线程正在按照您的要求工作

我对它进行了测试,它正在拍摄快照并将其保存到特定文件夹中

Option Explicit
'requires references: "Microsoft HTML Object Library" & "Microsoft Internet Controls"

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

Sub getSS()
  Const url = "stackoverflow.com" 'page to get screenshot of (http is added below)
  Const fName = "D:\thumb_" & url & ".png" 'output filename (can be png/jpg/bmp/gif)
  Const imgScale = 0.25 'scale to 25% (to create thumbnail)

  Dim ie As InternetExplorer, ws As Worksheet, sz As Long
  Dim img As Picture, oCht As ChartObject
  Set ws = ThisWorkbook.Sheets("Sheet1")
  Set ie = GetIE()
  With ie
    .navigate "http://" & url
    Do: DoEvents: Loop While .busy Or .readyState <> 4 'wait for page load
    ShowWindow Application.hwnd, 5 'activate IE window
    Call keybd_event(44, 0, 0, 0) '44="VK_SNAPSHOT"
    Pause (0.25) 'pause so clipboard catches up
    With ws
      ShowWindow Application.hwnd, 5 'back to Excel
      .Activate
      .Paste
      Set img = Selection
      With img
        Set oCht = ws.ChartObjects.Add(.Left, .Top, .Left + .Width, .Top + .Height)
        oCht.Width = .Width * imgScale 'scale obj to picture size
        oCht.Height = .Height * imgScale
        oCht.Activate
        ActiveChart.Paste
        ActiveChart.Export fName, Mid(fName, InStrRev(fName, ".") + 1)
        oCht.Delete
        .Delete
      End With
      .Activate
    End With
    .FullScreen = False
    .Quit
  End With
  If Dir(fName) = "" Then Stop 'Something went wrong (file not created)
  sz = FileLen(fName)
  If sz = 0 Then Stop 'Something went wrong! (invalid filename maybe?)
  Debug.Print "Created '" & fName & "' from '" & url & "' (" & sz & " bytes)": Beep
End Sub

Sub Pause(sec As Single)
  Dim t As Single: t = Timer
  Do: DoEvents: Loop Until Timer > t + sec
End Sub

Function GetIE() As Object
'requires references: "Microsoft HTML Object Library" & "Microsoft Internet Controls"
'return an object for the open Internet Explorer window, or create new one
  For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
    If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
  Next GetIE
  If GetIE Is Nothing Then Set GetIE=CreateObject("InternetExplorer.Application") 'Create
  GetIE.Visible = True 'Make IE visible
  GetIE.FullScreen = True
End Function
选项显式
'需要引用:“Microsoft HTML对象库”和“Microsoft Internet控件”
Private Declare Sub-keybd_事件库“user32”(ByVal bVk作为字节_
ByVal bScan为字节,ByVal dwFlags为长,ByVal dwExtraInfo为长)
私有声明函数ShowWindow Lib“user32”_
(ByVal hwnd尽可能长,ByVal nCmdShow尽可能长)尽可能长
子getSS()
Const url=“stackoverflow.com”页面以获取屏幕截图(下面添加了http)
Const fName=“D:\thumb\ux”&url&“.png”输出文件名(可以是png/jpg/bmp/gif)
常量imgScale=0.25'缩放至25%(用于创建缩略图)
Dim ie作为InternetExplorer,ws作为工作表,sz作为长
将img调暗为图片,将oCht调暗为图表对象
设置ws=ThisWorkbook.Sheets(“Sheet1”)
设置ie=GetIE()
与ie
。导航“http://”和url
Do:DoEvents:While.busy或.readyState 4'等待页面加载时循环
ShowWindow Application.hwnd,5'激活IE窗口
调用keybd_事件(44,0,0,0)'44=“VK_快照”
暂停(0.25)'暂停以便剪贴板跟上
与ws
ShowWindow Application.hwnd,5'返回Excel
.激活
粘贴
设置img=选择
与img
设置oCht=ws.ChartObjects.Add(.Left、.Top、.Left+.Width、.Top+.Height)
oCht.Width=.Width*imgScale“缩放对象到图片大小
oCht.Height=.Height*imgScale
哦,激活
活动图表。粘贴
ActiveChart.Export fName,Mid(fName,InStrRev(fName,“.”+1)
oCht.删除
.删除
以
.激活
以
.FullScreen=False
退出
以
如果Dir(fName)=“Then Stop”出错(文件未创建)
sz=FileLen(fName)
如果sz=0,则停止“出现问题!”!(可能是无效的文件名?)
Debug.Print“Created'&fName&“from'&url&'(&sz&“bytes”):嘟嘟声
端接头
子暂停(秒为单个)
将t调为单个:t=计时器
Do:DoEvents:循环直到计时器>t+秒
端接头
函数GetIE()作为对象
'需要引用:“Microsoft HTML对象库”和“Microsoft Internet控件”
'为打开的Internet Explorer窗口返回对象,或创建新对象
对于CreateObject(“Shell.Application”).Windows()循环中的每个GetIE
如果(不是格蒂什么都不是)