在 VBA 中,使用 Internet Explorer (IE) 时,可能需要等待弹出窗口出现
Microsoft Internet Controls
的引用。你可以在 "项目" > "引用" 中找到并添加它。Sub WaitForPopUpWindow()
Dim ie As InternetExplorer
Dim popUpWindow As Object
Dim popUpTitle As String
' 创建 Internet Explorer 实例并导航到指定 URL
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "https://example.com" ' 替换成你需要导航的网址
' 等待页面加载完成
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
' 设置弹出窗口标题
popUpTitle = "弹出窗口标题" ' 替换成你期望的弹出窗口标题
' 循环查找并等待弹出窗口出现
Do While True
Set popUpWindow = FindWindow(0, popUpTitle)
If Not popUpWindow Is Nothing Then
Exit Do
Else
DoEvents
End If
Loop
' 弹出窗口已找到,你可以在这里继续执行其他操作
' 关闭 Internet Explorer 实例
ie.Quit
Set ie = Nothing
End Sub
' 使用 Windows API 函数 FindWindow 来查找窗口
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
这段代码首先创建了一个 IE 实例并导航到指定的 URL。然后,它等待页面加载完成。接着,它使用 FindWindow
函数循环查找具有指定窗口标题的弹出窗口。一旦找到弹出窗口,循环就会终止,然后你可以继续执行其他操作。最后,关闭 IE 实例。
请注意,这种方法依赖于弹出窗口的标题与提供的 popUpTitle
参数相匹配。因此,请确保将 popUpTitle
变量设置为与弹出窗口标题匹配的字符串。
领取专属 10元无门槛券
手把手带您无忧上云