希望在 VBA 的范围内完成此操作(其他用户没有其他开发工具可以修改)。我知道第三方应用程序(例如 iMacros)具有类似功能,但希望尽可能通用。本店使用XP和Excel 2003。
(1) VBA 子例程控制 InternetExplorer 浏览器以自动查看网站、表单提交等。
(2) 有没有办法从网络浏览器的内容中获取屏幕截图?没有困惑的 SendKeys 方法? .NET 有一个 Webbrowser.DrawToBitmap 方法,但找不到 VBA 的简单解决方案。想要整个屏幕,包括“折叠下方”-滚动条下方...
最佳答案
将完整代码粘贴到模块中并运行 Sub Sample()
代码逻辑
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
关于VBA WebBrowser捕获全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10759580/