Automation of IE, Automation Error

BrianO

New Member
Joined
Mar 13, 2009
Messages
47
I have been working on a project which requires me to populate a webform from Excel. I have managed to get this working 90% of the time. The other 10% I'm getting an automation error

Run-time error '-2147417848 (80010108)':
Automation Error
The object involked has disconnected from its clients.

This is the basis of the code that I am using to populate the webform

Dim refcamp As String

refcamp = Worksheets("SheetName").Range("D8").Value

Set ie2 = CreateObject("InternetExplorer.Application")
With ie2
.Visible = True
.AddressBar = False
.Navigate "http://www.website.com

Do Until .ReadyState = 4: DoEvents: Loop
End With

Set Campaign = ie2.Document.all.Item("refcamp")
Campaign.Value = refcamp

ie2.Document.forms(0).submit

the automation error usually happens at one of the follow lines:
".Navigate "http://www.website.com"
".AddressBar = False"
"Do Until .ReadyState = 4: DoEvents: Loop"

I am looking for a way to handle this error.
Would putting On Error GOTO ErrorHandling before all this code work or do I need to specify the error that is occuring.

Also is there a way of applying this error check to an entire module.

Regards,
B
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
this is what I have come up with so far


Code:
On Error Goto ErrorCheck
Dim refcamp As String
 
refcamp = Worksheets("SheetName").Range("D8").Value
 
Set ie2 = CreateObject("InternetExplorer.Application")
With ie2
.Visible = True
.AddressBar = False
.Navigate "[URL="http://www.website.com/"]http://www.website.com[/URL]
 
Do Until .ReadyState = 4: DoEvents: Loop
End With
 
Set Campaign = ie2.Document.all.Item("refcamp")
Campaign.Value = refcamp
 
ie2.Document.forms(0).submit 
 
ErrorCheck:
    ReferralErrorCheck = True
    frmWarning.lblWarning.Caption = "Error Message"
    Load frmWarning
    frmWarning.Show
    Exit Sub

I haven't used On Error Before. Is this the correct way to construct the code
 
Upvote 0
I have put this in place and have done some testing and it seems to be working. Does anyone know if this a good way to handle this error or should I be doing this a different way?
 
Upvote 0
Given your code as presented here, you probably need an Exit Sub or GoTo AnotherLabel immediately after the ie2.Document.forms(0).submit line so that the error handler (the ErrorCheck code) is not executed when an error doesn't occur.

Also, if you are happy that the error occurs at certain statements, put the On Error GoTo ErrorCheck as close as possible to the start of those statements, followed by On Error GoTo 0 after them to turn off error handling. Otherwise the error handler would be triggered for other, unexpected errors, hiding the cause of them. Whilst developing your program you want VBA's default error handler to display the normal error message dialogue box so that you can see and code for such errors, if necessary.

To check for specific errors, look at the Err object, specifically Err.Number and Err.Description. For more on VBA error handling, see http://www.cpearson.com/excel/ErrorHandling.htm

Generally though, as I mentioned, it's better to anticipate and code for possible errors to prevent them happening in the first place, rather than handling them with On Error.
 
Upvote 0
you probably need an Exit Sub or GoTo AnotherLabel immediately after the ie2.Document.forms(0).submit line so that the error handler (the ErrorCheck code) is not executed when an error doesn't occur.

I will put this in. I hadn't actually noticed it running the ErrorCheck code but good idea.

Generally though, as I mentioned, it's better to anticipate and code for possible errors to prevent them happening in the first place, rather than handling them with On Error.

The Automation error seems to happen quite randomly and I cant pinpoint a line of code where it is happening. It seems to be that it can happen on any line that is controlling IE. If you have any ideas as to how to pinpoint why the error is occuring I would be greatful as then I could possibly add code to deal with it.

Thanks for the response.
B
 
Upvote 0
The Automation error seems to happen quite randomly and I cant pinpoint a line of code where it is happening. It seems to be that it can happen on any line that is controlling IE. If you have any ideas as to how to pinpoint why the error is occuring I would be greatful as then I could possibly add code to deal with it.

Thanks for the response.
B
Put 'trace statements' in your code with Debug.Print "xxx" at various points and verify that the program flow is as expected, in relation to what IE is doing and what your VBA code has instructed it to do.

Without seeing the whole program, or enough of it to reproduce the problem, it's difficult to pinpoint the error to a specific part of the code.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top