How to take screenshot of webpage using VBA?

2019-01-24 22:28发布

How to take screenshot of webpage using VBA in excel. The problem is that screen shots can be taken only by pressing F6 key of keyboard since Screenhunter is used for this purpose. The print screen key is disabled.

I used the following code but realised that sendkey function can not be used:

    sub test()

       application.sendkeys "{F6}"

    end sub

Please Help.

1条回答
狗以群分
2楼-- · 2019-01-24 23:06

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
查看更多
登录 后发表回答