ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何运用VBA模拟第二个线程去获取WEBBROWSER中的弹出ALERT窗口的文本

如何运用VBA模拟第二个线程去获取WEBBROWSER中的弹出ALERT窗口的文本

作者:绿色风 分类: 时间:2022-08-17 浏览:189
楼主
xmyjk
Q:由于VBA是单线程的,无法用API函数FINDWINDOW去获取其窗体中WEBBROWSER控件中网页的弹出ALERT的窗口。如果模拟创造另一个线程,去获取这个弹出窗口的文本值呢?

A:运用计时器TIMER来模拟创造另一个线程来解决,
注意:

 
如上图,我的IE的弹出警告窗的标题栏是Windows Internet Explorer(一般为Windows Internet Explorer;来自网页的消息;安全警报;安全警告等等),请按情况修改下面的代码“hWnd = FindWindowEx(0&, 0&, vbNullString, ByVal "Windows Internet Explorer")这句和FindWindowEx(0&, hMessageBox, vbNullString, ByVal "Windows Internet Explorer")这句”。
代码如下:
窗体代码:
  1. Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
  2. Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
  3. Option Explicit
  4. Public a&
  5. Private Sub UserForm_Activate()
  6.     WebBrowser1.Navigate2 ThisWorkbook.Path & "\1111.html"
  7.     a = SetTimer(0, 0, 100, AddressOf CloseMessageTimerProc)
  8. End Sub

  9. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  10.     KillTimer 0, a
  11. End Sub
标准模块代码:
  1. Option Explicit
  2. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As Any) As Long
  3. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  4. Public flag As Boolean
  5. Sub ttt()
  6.     flag = False
  7.     UserForm1.Show
  8. End Sub
  9. Sub CloseMessageTimerProc()
  10.     Dim hMessageBox As Long
  11.     Debug.Print flag
  12.     If flag Then Exit Sub
  13.     flag = True
  14.     hMessageBox = FindWindowEx(0&, 0&, vbNullString, ByVal "Windows Internet Explorer")
  15.     Do While hMessageBox
  16.         ClickMessageBox hMessageBox
  17.         hMessageBox = FindWindowEx(0&, hMessageBox, vbNullString, ByVal "Windows Internet Explorer")
  18.     Loop
  19. End Sub
  20. Private Sub ClickMessageBox(ByVal phwnd As Long)
  21.     Dim wndtext As String, AllWndText As String, i As Long, fhwnd As Long
  22.     wndtext = Space(512)
  23.     fhwnd = phwnd
  24.     phwnd = FindWindowEx(fhwnd, 0&, vbNullString, 0&)
  25.     Do While phwnd > 0
  26.         i = GetWindowText(phwnd, wndtext, 512)
  27.         If i Then AllWndText = AllWndText & Left(wndtext, i) & "-"
  28.         phwnd = FindWindowEx(fhwnd, phwnd, vbNullString, 0&)
  29.     Loop
  30.     [a1] = Replace(AllWndText, Chr(0), "")
  31.     SendKeys "~"
  32. End Sub

webbrowser2线程.zip
2楼
eliane_lei
虽然V白,看不懂的成份居多,但是还是要跟着楼主好好学习!
3楼
bishunbiao
跟着师傅学习
4楼
su0nils000
貌似通过子窗口是找不到弹出窗口的吧?弹出窗口应该也是顶层窗口吗?
5楼
老糊涂
学习

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap