VBA WebBrowser 捕获全屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10759580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA WebBrowser capture full screen
提问by SteveValarenti
Would like to do this within the confines of VBA (other users have no other development tools to modify). Am aware of 3rd party apps (iMacros, for example) that do similar but want to have this as generic as possible. The shop uses XP and Excel 2003.
想在 VBA 的范围内执行此操作(其他用户没有其他开发工具可修改)。我知道 3rd 方应用程序(例如 iMacros)具有类似的功能,但希望尽可能通用。该商店使用 XP 和 Excel 2003。
(1)A VBA subroutine is controlling an InternetExplorer browser to automate viewing website, form submits, etc.
(1)VBA 子程序控制 InternetExplorer 浏览器以自动查看网站、表单提交等。
(2)Is there a way to get a screen capture from the contents of the WebBrowser? Without messy SendKeys approaches? .NET has a Webbrowser.DrawToBitmap method but can't find an easy solution for VBA. Want the entire screen, including "below the fold" - below the scroll bars...
(2)有没有办法从 WebBrowser 的内容中获取屏幕截图?没有凌乱的 SendKeys 方法?.NET 有一个 Webbrowser.DrawToBitmap 方法,但找不到适用于 VBA 的简单解决方案。想要整个屏幕,包括“折叠下方” - 在滚动条下方......
回答by Siddharth Rout
TRIED AND TESTED (Paste complete code in a module and run Sub Sample()
尝试和测试(将完整代码粘贴到模块中并运行 Sub Sample()
CODE LOGIC
代码逻辑
1)This code will open IE
1)此代码将打开 IE
2)Navigate to Google.com
2)导航到 Google.com
3)Maximize IE
3)最大化IE
4)Take snapshot
4)拍摄快照
5)Launch MSPaint
5)启动 MSPaint
6)Paste in 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
回答by rchacko
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