我遇到一个问题,间歇性地这个过程将无法找到一个打开的IE窗口。正如您所看到的,我调试了每个打开窗口的URL,因此很容易看到窗口根本不包含在它循环的对象列表中。关闭Excel并重新打开它似乎解决了这个问题,但我似乎无法找到导致它的原因。有任何想法吗?或者,是否有另一个进程可用于按名称或URL设置现有IE对象?
Public Function GetWindow(name As String)
Dim ie As InternetExplorer
Dim l As Integer
Dim TestStr As String
Dim ShellW As ShellWindows
Set ShellW = New ShellWindows
On Error Resume Next
If ShellW.Count > 0 Then
For l = 0 To ShellW.Count - 1
TestStr = ""
TestStr = ShellW(l).LocationURL
If TestStr <> "" Then
Debug.Print TestStr
End If
If InStr(TestStr, name) > 0 Then
If ShellW(l).Visible = True Then
If ie Is Nothing Then Set ie = ShellW(l): Exit For
End If
End If
Next l
End If
Set GetWindow = ie
End Function