Thomasvba
Thomasvba

Reputation: 3

Run time error 91 - IE Object

The code works correctly but when I try to close IE I get the following error:

"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

I try to read the url from tabs open in a IE session to export it in a sheet.

I'm so sorry but i'm a newbie in a VBA :-(

Upvotes: 0

Views: 654

Answers (1)

SierraOscar
SierraOscar

Reputation: 17637

IE isn't actually set to an instance of Internet Explorer at any point, you're using it as an object variant in a loop. Once the loop finishes, the variables becomes out of scope, and therefore cannot be accessed any more in the way you would like.

If you want to close IE after retrieving the URL, change your code to:

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

Note there is no need to Set IE = Nothing at the end of your routine as VBA automatically manages memory for you.

Upvotes: 1

Related Questions