How to take screenshot of webpage using VBA?

前端 未结 2 1564
悲&欢浪女
悲&欢浪女 2020-12-06 03:52

How to take screenshot of webpage using VBA in Excel? The problem is that screenshots can be taken only by pressing F6 key of keyboard since Screenhunter is used for this pu

2条回答
  •  难免孤独
    2020-12-06 04:32

    I have added some delay after maximizing the screen, created a Word Doc and pasted the screen shot in it. The rest of the code is taken from the link which Siddharth has provided.

    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 Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    
    Private Const VK_SNAPSHOT As Byte = 44
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
    Private Const SW_SHOWMAXIMIZED = 3
    Private Const VK_LCONTROL As Long = &HA2
    Private Const VK_V = &H56
    Private Const KEYEVENTF_KEYUP = &H2
    
    Sub Sample()
        Dim IE As Object
        Dim hwnd As Long, IECaption As String
    
        Set IE = CreateObject("InternetExplorer.Application")
    
        IE.Visible = True
    
        IE.Navigate "www.Google.com"
    
        Sleep 5000
    
        '~~> Get the caption of IE
        IECaption = "Google - Internet Explorer"
    
        '~~> Get handle of IE
        hwnd = FindWindow(vbNullString, IECaption)
    
        If hwnd = 0 Then
            MsgBox "IE Window Not found!"
            Exit Sub
        Else
            '~~> Maximize IE
            ShowWindow hwnd, SW_SHOWMAXIMIZED
        End If
    Sleep 3000
        DoEvents
    
        '~~> Take a snapshot
        Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
    
        '~~> Start Word
    
    
        Set wordobj = CreateObject("Word.Application")
    
       Set objDoc = wordobj.Documents.Add
    
       wordobj.Visible = True
    
       Set objSelection = wordobj.Selection
    
       'Paste into Word
       objSelection.Paste
    
    End Sub
    

提交回复
热议问题