Quint
Quint

Reputation: 590

VBA: Error with in an error handler

I have a problem with my error handler. I have a script that gains access to a site and downloads a few files. I want to implement an error handler for it because a change to the site could cause errors to be thrown. This is my error handler.

IEError:
    TxtLog ExtractErrLogDir, Environ("UserName") & " " & Now() & " " & Err.Number & ":" & Err.Description ' this is a function I made that writes to txt files
    On Error Resume Next
    IE.Quit
    Set IE = Nothing
    MsgBox "Auto Extract Stop Working:" & vbNewLine & Err.Description

End Sub

Also, How I make a new instance of IE.

Set IE = New InternetExplorerMedium

The problem I have is I have no way to tell if the Internet explorer instance I made is closed or not for my error handler. So I attempted to make an error handler with in the error handler. To close IE and if it errors on that ignore it and move to next line. But its not working correctly IE.Quit will still throw and error out side of the handler.

Upvotes: 1

Views: 656

Answers (2)

Siddharth Rout
Siddharth Rout

Reputation: 149295

This is how I do it

LetsContinue:
    On Error Resume Next
    IE.Quit
    Set IE = Nothing
    MsgBox "Auto Extract Stop Working:" & vbNewLine & Err.Description
    Exit Sub
IEError:
    TxtLog ExtractErrLogDir, Environ("UserName") & " " & Now() & " " & _
    Err.Number & ":" & Err.Description
    
    Resume LetsContinue

To create a new instance of IE using Late Binding, use this

Dim IE As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True

To create a new instance of IE using Early Binding, use this. You will need to set the reference to Microsoft Internet Controls

Dim IE As New InternetExplorer

IE.Visible = True

EDIT

What is the advantage of Dim IE As New InternetExplorer vs how I have it? – Quint 12 mins ago

InternetExplorerMedium was first used way back when Vista and IE8 was launched to create an instance of Internet Explorer running at a medium integrity level.

I see no need of it in today's era.

See the remarks section from this MSDN KB

Remarks

Windows Internet Explorer 8. On Windows Vista, to create an instance of Internet Explorer running at a medium integrity level, pass CLSID_InternetExplorerMedium (defined in exdisp.idl) to CoCreateInstance. The resulting InternetExplorerMedium object supports the same events, methods, and properties as the InternetExplorer object.

I am not sure, how did I forget to mention this but instead of using On Error Resume Next you can also do this

LetsContinue:
    If Not IE Is Nothing Then
        IE.Quit
        Set IE = Nothing
    End If
    '~~> Not sure what is the below line for but I have still included it
    MsgBox "Auto Extract Stop Working:" & vbNewLine & Err.Description
    Exit Sub
IEError:
    TxtLog ExtractErrLogDir, Environ("UserName") & " " & Now() & " " & _
    Err.Number & ":" & Err.Description
    
    Resume LetsContinue

Upvotes: 2

A.S.H
A.S.H

Reputation: 29332

After an error is thrown within a routine, you need the call Resume before setting up a new error-handling mechanism. Try it this way:

IEError:
    TxtLog ExtractErrLogDir, Environ("UserName") & " " & Now() & " " & Err.Number & ":" & Err.Description ' this is a function I made that writes to txt files
    Resume TryQuit ' <-- call Resume to be able to setup new err-handling

TryQuit:
    On Error Resume Next
    IE.Quit
    Set IE = Nothing
    MsgBox "Auto Extract Stop Working:" & vbNewLine & Err.Description
End Sub

Upvotes: 1

Related Questions