VBA WebBrowser捕获全屏

时间:2012-05-25 18:23:58

标签: excel vba internet-explorer-8 excel-vba browser

想在VBA的范围内做到这一点(其他用户没有其他开发工具可以修改)。我知道第三方应用程序(例如iMacros)做类似但希望尽可能通用。该商店使用XP和Excel 2003。

(1) VBA子程序正在控制InternetExplorer浏览器自动查看网站,表单提交等。

(2)有没有办法从WebBrowser的内容中获取屏幕截图?没有凌乱的SendKeys方法? .NET有一个Webbrowser.DrawToBitmap方法,但无法找到一个简单的VBA解决方案。想要整个屏幕,包括“在屏幕下方” - 滚动条下方......

2 个答案:

答案 0 :(得分:10)

测试和测试(在模块中粘贴完整代码并运行子样本()

CODE LOGIC

1)此代码将打开IE

2)导航至Google.com

3)最大化IE

4)拍摄快照

5)启动MSPaint

6)粘贴在MSPaint中

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 - Windows 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

    DoEvents

    '~~> Take a snapshot
    Call keybd_event(VK_SNAPSHOT, 0, 0, 0)

    '~~> Start Paint
    Shell "C:\Windows\System32\mspaint.exe", vbNormalFocus

    Sleep 3000

    '~~> Paste snapshot in paint
    keybd_event VK_LCONTROL, 0, 0, 0
    keybd_event VK_V, 0, 0, 0
    keybd_event VK_V, 0, KEYEVENTF_KEYUP, 0
    keybd_event VK_LCONTROL, 0, KEYEVENTF_KEYUP, 0
End Sub

答案 1 :(得分:2)

Dim objIe As Object
Set objIe = CreateObject("internetexplorer.application")
    With objIe
        .Navigate "www.google.com"
        '// Set offline JIC user NOT Online
        .offline = True
        '// Maximise the Ie window if not Already Max
        .Visible = True
        '// This routine used to Maximise Ie
        ShowWindow objIe.hwnd, SW_MAXIMIZE
        SetForegroundWindow objIe.hwnd
    End With
  

Public Const SW_MAXIMIZE As Long = 3       'Show window Maximised
Public Const SW_MINIMIZE As Long = 1       'Show window Minimized

Public Declare Function ShowWindow _
    Lib "user32" ( _
    ByVal hwnd As Long, _
    ByVal nCmdShow As Long) _
As Long

Public Declare Function SetForegroundWindow _
    Lib "user32" ( _
    ByVal hwnd As Long) _
As Long