使用 VBA 定位现有 IE window 选项卡
Target existing IE window tab using VBA
我正在尝试使用我的脚本针对现有的 IE window(仅一个选项卡)。
换句话说:有一个 Internet Explorer window 打开了一个特定的选项卡,我试图让我的脚本使用它的选项卡名称来定位该选项卡。
我查看了这些主题,但 none 对我有用(或者我做错了):
- Get existing IE via VBA
- VBA Macro For Already Open IE Window
因此我创建了这个(它工作了一段时间直到我无缘无故地收到 438 错误):
Sub FindingExistingIE()
Dim Application As Object
Dim ApplicationWindows As Object
Dim WindowTitle As Variant
Dim TargetWindow As Object
Set Application = CreateObject("Shell.Application")
Set ApplicationWindows = Application.Windows
For Each Application In ApplicationWindows
WindowTitle = Application.Document.Title
If WindowTitle Like "IE Window Title" Then
Set TargetWindow = Application
Exit For
End If
Next Application
NextSub TargetWindow
End
End Sub
正如我所说,脚本在一周内运行良好,但现在我收到 438 错误:
Object doesn't support this property or method (Error 438)
错误突出显示以下行:
WindowTitle = Application.Document.Title
阅读 Microsoft Doc related to the error 后,我尝试将 WindowTitle 变量更改为一个对象。没用。
有人知道吗?
谢谢!
这对我有用 - 通过查找打开的 window 和传递的 URL:
来找到 IE
Sub tester()
Dim w As Object
Set w = FindingExistingIEByUrl("https://www.google.com")
If Not w Is Nothing Then
MsgBox w.document.Title
Else
MsgBox "No IE window found"
End If
End Sub
Function FindingExistingIEByUrl(theUrl)
Dim w As Object
For Each w In CreateObject("Shell.Application").Windows
'Debug.Print w.locationurl
If w.locationurl Like theUrl & "*" Then
Set FindingExistingIEByUrl= w
Exit Function
End If
Next w
End Function
Function FindingExistingIEByTitle(theTitle)
Dim w As Object, t As String
For Each w In CreateObject("Shell.Application").Windows
t = ""
On Error Resume Next 'ignore error if no Document
t = w.Document.Title
On Error Goto 0
If t Like theTitle Then
Set FindingExistingIEByTitle = w
Exit Function
End If
Next w
End Function
我正在尝试使用我的脚本针对现有的 IE window(仅一个选项卡)。
换句话说:有一个 Internet Explorer window 打开了一个特定的选项卡,我试图让我的脚本使用它的选项卡名称来定位该选项卡。 我查看了这些主题,但 none 对我有用(或者我做错了):
- Get existing IE via VBA
- VBA Macro For Already Open IE Window
因此我创建了这个(它工作了一段时间直到我无缘无故地收到 438 错误):
Sub FindingExistingIE()
Dim Application As Object
Dim ApplicationWindows As Object
Dim WindowTitle As Variant
Dim TargetWindow As Object
Set Application = CreateObject("Shell.Application")
Set ApplicationWindows = Application.Windows
For Each Application In ApplicationWindows
WindowTitle = Application.Document.Title
If WindowTitle Like "IE Window Title" Then
Set TargetWindow = Application
Exit For
End If
Next Application
NextSub TargetWindow
End
End Sub
正如我所说,脚本在一周内运行良好,但现在我收到 438 错误:
Object doesn't support this property or method (Error 438)
错误突出显示以下行:
WindowTitle = Application.Document.Title
阅读 Microsoft Doc related to the error 后,我尝试将 WindowTitle 变量更改为一个对象。没用。
有人知道吗?
谢谢!
这对我有用 - 通过查找打开的 window 和传递的 URL:
来找到 IESub tester()
Dim w As Object
Set w = FindingExistingIEByUrl("https://www.google.com")
If Not w Is Nothing Then
MsgBox w.document.Title
Else
MsgBox "No IE window found"
End If
End Sub
Function FindingExistingIEByUrl(theUrl)
Dim w As Object
For Each w In CreateObject("Shell.Application").Windows
'Debug.Print w.locationurl
If w.locationurl Like theUrl & "*" Then
Set FindingExistingIEByUrl= w
Exit Function
End If
Next w
End Function
Function FindingExistingIEByTitle(theTitle)
Dim w As Object, t As String
For Each w In CreateObject("Shell.Application").Windows
t = ""
On Error Resume Next 'ignore error if no Document
t = w.Document.Title
On Error Goto 0
If t Like theTitle Then
Set FindingExistingIEByTitle = w
Exit Function
End If
Next w
End Function