退出错误Internet Explorer窗口

时间:2016-11-17 14:02:38

标签: windows vba internet-explorer

问题:我目前遇到一些关闭“过早”互联网窗口的问题,该窗口尚无内容。

prematured window

基本上如果这个“过早”窗口打开,我的宏无法选择我感兴趣的窗口,但只要我手动关闭这个“过早”窗口,我就可以正确运行我的代码。 它的来源是一个小错误,它以某种方式打开它,但除了diplaying该窗口不会影响代码的其余部分。

完成测试:

Dim Widow As Object, page_foireuse As SHDocVw.InternetExplorer
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")

     MsgBox objShell.Windows.Count
'    MsgBox objShell.Windows(0).document.Title
'    MsgBox objShell.Windows(1).document.Title
'    MsgBox objShell.Windows(2).document.Title
'    MsgBox objShell.Windows(3).document.Title
'    MsgBox objShell.Windows(4).document.Title
'    MsgBox objShell.Windows(5).document.Title
'    MsgBox objShell.Windows(6).document.Title


'For Each Widow In objShell.Windows
'    If Widow.document.Title Is Nothing Then ' this doesn't work
'        Set page_foireuse = Widow
'    End If
'Next

'
'If objShell.Windows(5).document.Title Is Nothing Then
'End If
        Set page_foireuse = objShell.Windows(5)
    page_foireuse.Quit

 MsgBox objShell.Windows.Count

到目前为止的结果:

  1. 当我计算shell中的窗口数时,这个“过早”窗口也会计算
  2. 当我返回每个计算窗口的位置或标题时,我在尝试返回“过早”窗口的位置或标题时出错
    1. 我试图在此端运行的两个循环不起作用
  3. 所以我的问题是:如何通过宏关闭这个“过早”窗口?

1 个答案:

答案 0 :(得分:1)

您问题中图片上的页面看起来很奇怪,没有位置,没有标题。请尝试以下代码,但在Set doc = ie.document行设置断点并检查doc是否不是Nothing等.HPH

' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library
' Add reference to Microsoft Shell Controls And Automation

Dim ie As SHDocVw.WebBrowser
Dim doc As HTMLDocument
Dim shellApp As Shell32.Shell
Dim windows As SHDocVw.ShellWindows
Dim window

Set shellApp = New Shell
Set windows = shellApp.windows

For Each window In windows
    If Not UCase(window.FullName) Like "*IEXPLORE.EXE" Then GoTo continue
    Set ie = window
    Set doc = ie.document
    If doc.Title = "" Then
        ie.Quit
        Exit For
    End If
continue:
Next window