运行时错误91 - IE对象
问题描述:
代码工作正常,但是当我试图关闭IE浏览器,我得到以下错误:运行时错误91 - IE对象
"Run Time Error 91"
Dim IE As Object
Dim shellWins As New ShellWindows
Dim IE_URL As String
Dim Row As Integer
Row = 2
For Each IE In shellWins
IE_URL = IE.LocationURL
If IE_URL <> vbNullString Then
Sheet1.Range("A" & Row) = IE_URL
row = Row + 1
End If
Next
Set shellWins = Nothing
Set IE = Nothing
IE.quit
NEW CODE:
Dim shellWins As New ShellWindows
Dim objIE As Object
Dim objIE_TabURL As String
Dim Rowcount As Long
Rowcount = 2
With objIE
Set objIE = CreateObject("InternetExplorer.Application")
For Each objIE In shellWins
objIE_TabURL = objIE.LocationURL
If objIE_TabURL <> vbNullString And InStr(objIE_TabURL,
"file://") = 0 Then
Rowcount = Rowcount + 1
sht2.Range("A" & Rowcount) = objIE_TabURL
End If
Next
objIE.Quit
End With
END NEW CODE
我尝试从IE会话中打开的标签中读取URL以将其导出到工作表中。
我很抱歉,但我在VBA新手:-(
答
IE
实际上并没有设置在任何一点的Internet Explorer的一个实例,你使用它作为一个对象变种一旦循环完成,变量将超出范围,因此无法以您想要的方式再次访问。
如果要在检索URL后关闭IE,请将您的代码更改为:
For Each IE In shellWins
IE_URL = IE.LocationURL
If IE_URL <> vbNullString Then
Sheet1.Range("A" & Row) = IE_URL
row = Row + 1
IE.Quit '// Close IE here, while the variable is still in scope
End If
Next
注意不需要Set IE = Nothing
在您的例程结束时,VBA自动为您管理内存。
您在退出之前设置IE = Nothing',因此您杀死了IE对象,之后您无法再访问它。在设置'IE = Nothing'之前退出应该工作。 –
@Peh实际上,“IE”甚至没有设置为循环之外的Internet Explorer实例,因此无法工作。 –
@MacroMan你是完全正确的。只能看到与错误消息匹配的最终问题。 –