Internet-Explorer 在我的模块结束后不会关闭

Internet-Explorer won't get closed after my module ends

我有这个从网站提取数据的宏。我从用户那里得到输入。它可以正常工作,但 IE 不会关闭并占用所有内存。除了 IE.Quit 之外还需要其他东西吗?

这是潜艇。如您所见,我最后关闭了 IE。

Public Cancel As Boolean

Sub USGD()

Dim IE As Object
Dim iWsh As Worksheet
Dim link As String
Dim sDate As String
Dim eDate As String
Dim StationID As String
    
Cancel = False

With USGS
    .StartUpPosition = 0
    .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
    .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    .Show
End With

If Cancel = True Then
    Unload USGS
    Exit Sub
End If

With ActiveWorkbook
    Set iWsh = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With

iWsh.Activate
iWsh.Range("A1").Select 'I know this is not efficient but works fine


 StationID = USGS.TextBox1.Text
'StationID = InputBox("Please enter the station ID")
    
'sDate = InputBox("Please enter START date in this format: 'yyyy-mm-dd'")
'eDate = InputBox("Please enter END date in this format: 'yyyy-mm-dd'")
 sDate = Format(USGS.TextBox2.Text, "yyyy-mm-dd")
 eDate = Format(USGS.TextBox3.Text, "yyyy-mm-dd")
 
 
link = "https://waterdata.usgs.gov/ & _ 
StationID & sDate & eDate
 
 
 Unload USGS
 
Set IE = CreateObject("InternetExplorer.Application")
With IE
           .Visible = False
           .Navigate link 'URL
           
  Do Until .ReadyState = 4: DoEvents: Loop
  
           .ExecWB 17, 0 '// SelectAll
           .ExecWB 12, 2 '// Copy selection
End With
     

    iWsh.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
    Range("A1").Select
    
    IE.Quit
    Set IE = Nothing
    Set iWsh = Nothing
    
End Sub

这是用户表单:我在使用 Input-Box 时没有遇到这个问题,所以我猜,它与用户表单有关。这仅在用户关闭用户表单时发生。

Private Sub ToggleButton1_Click()
Me.Hide
    Cancel = True
End Sub

Private Sub OK_Click()
Me.Hide
End Sub

注意:如果用户取消,它甚至不会打开IE并立即退出子。

但是 如果用户关闭表单,它会打开 IE,并且不会将 Cancel 设置为 True,这是退出子程序的条件。

更新: Expert-Exchange 涵盖了这个问题,但从未提出实际的解决方案。

Update-2: 关闭所有 IE 实例不是一个选项。

现在用户表单是这样设置的:

我找到了解决方法,但不是一个好的答案。另外,没有解释为什么会这样。

我更改了我的用户表单,因此用户无法关闭它。同时将 Cancel 的初始值设置为 True

Cancel = True

并且当用户按下 OK 时,它将被设置为 false。在下面查找用户表单代码:

Private Sub ToggleButton1_Click()

End Sub

Private Sub OK_Click()
    Me.Hide
    Cancel = False
End Sub

这样做的明显缺点是用户无法关闭用户表单;他们必须点击 OK。这不会导致错误。我可以添加一个条件,如果其中一个文本框为空,则退出子。如下所示;

If sDate = "" Or eDate = "" Or StationID = "" Then GoTo 92
'. Rest of ...
'. My ... 
'. Code ...
92:
           Set IE = Nothing

End Sub

好的,所以我无法重现错误,所以您应该尝试两件事:

  1. 重新启动计算机并确认错误仍然存​​在。如果没有,问题已解决。
  2. 新的空白 工作簿中重新创建用户窗体和代码,并查看错误是否仍然存在。如果没有,问题已解决。

(有时工作簿,and/or 用户表单损坏)

我还稍微重构了代码,即使上述建议之一解决了问题,您也可以考虑。它只是稍微清理了一下,让它更有目的性。

在标准模块中,放置以下代码:

USGD 过程显示用户窗体并卸载它。名为 GetData 的单独过程将在 IE 中完成工作并添加工作表等。只有当用户单击窗体上的 "OK" 按钮时,才会执行 GetData 过程。因此,"X"/取消按钮将允许用户关闭表单。

Option Explicit

Sub USGD()
'Procedure displays the userform for the user
Dim USGSForm As New USGS
With USGSForm
    .StartUpPosition = 0
    .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
    .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    .Show
End With
Unload USGSForm
End Sub

Sub GetData(StationID As String, sDate As String, eDate As String)
'This procedure queries the InternetExplorer for the values from UserForm
Dim iWsh As Worksheet
Dim link As String
Dim IE As Object

sDate = Format(sDate, "yyyy-mm-dd")
eDate = Format(eDate, "yyyy-mm-dd")

link = "https://waterdata.usgs.gov/nwis/dv?cb_00060=on&format=rdb&site_no=" & _
    StationID & "&referred_module=sw&period=&begin_date=" & sDate & "&end_date=" & eDate

Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = False
    .Navigate link 'URL
    Do Until .ReadyState = 4: DoEvents: Loop
    .ExecWB 17, 0 '// SelectAll
    .ExecWB 12, 2 '// Copy selection
    .Quit
End With

With ActiveWorkbook
    Set iWsh = .Sheets.Add(After:=.Sheets(.Sheets.Count))
End With
iWsh.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
Application.GoTo iWsh.Range("A1")

End Sub

在您的用户窗体模块中,放置以下代码:

这是 "OK" 按钮的代码,它从表单上的文本框中获取值并将这些值发送到 GetData 过程。请注意 Select Case 逻辑,如果 any 参数为空,它将提前退出过程,因此它将 not 调用 GetData.

Private Sub OK_Click()
    Dim id As String, sDate As String, eDate As String
    'Get values from the form
    id = Me.TextBox1.Value
    sDate = Me.TextBox2.Value
    eDate = Me.TextBox3.Value
    'Hide the form
    Me.Hide

    'If ANY required parameter is blank, this results in malformed URL so exit the procedure
    Select Case vbNullString
        Case id, sDate, eDate
            MsgBox "You left some parameter blank, no query will be performed.", vbInformation
            GoTo EarlyExit
        Case Else
            'Send values to the procedure that queries IE
            Call GetData(id, sDate, eDate)
    End Select

EarlyExit:
End Sub