Link to home
Start Free TrialLog in
Avatar of broadbent
broadbentFlag for United Kingdom of Great Britain and Northern Ireland

asked on

OCX errors

I have written an OCX which accesses the Outlook object model.

However if Outlook hasn't been loaded, then presumably the CreateObject method will cause an error to be raised insided the ocx.

How do I prevent error messages like this from affecting the app which contains the ocx?

On error goto inside the ocx does not work, and app.oleserver etc is illegal as the ocx is not an exe.
Avatar of ameba
ameba
Flag of Croatia image

In your OCX, you need error handler to trap the error. When error happens, raise your own error and pass it to your caller application.
Public Sub MyOCXSub()
    On Error Go To EH
    ' your code

    Exit Sub

EH:
    On Error Go To 0
    Err.Raise vbObjectError + 1021, "Description", App.Name & "ModuleName.MyOCXSub"
End Sub


Caller application can show error to the user, or ignore it or retry.
Public Sub Command1_Click()
    On Error Go To EH
    Call MyOCXSub()
    Exit Sub

EH:
    MsgBox "Error: " & Err.Description & " in " & Err.Source
End Sub
Avatar of troywillmot
troywillmot

On error should work regardless of the project type, infact I've used on error inside ocx's.

If its not working, try a couple of things :

1. Check the in the Tools->Options menu your error trapping is set to 'Break On Unhandled Errors'

2. after any error, regardless of whether you have

  on error goto somewhere

or just

  on error resume next

In most cases where people complain on error goto isn't working, they have their error trapping mode in VB set to 'Break In Class Module' or 'Break On All Errors'. This means that error traps are ignored, depending on where an error occurs.

UserControls and forms are considfered class modules it seems (they are objects after all), and so if your set to 'Break In Class Module', its still going to ignore errors in the usercontrol.

Setting it to 'Break On Unhandled Errors' should make it work.


either resume to a known location, i.e

  resume NextOne

NextOne:

or do an Err.Clear.

Avatar of broadbent

ASKER

I'm sorry but the on error doesn't work.

I have

on error goto err
getobject("whatever")

exit sub

err
err.clear
on error goto oleerr
createobject("whatever")
exit sub

olerr:
some thing
end sub
ASKER CERTIFIED SOLUTION
Avatar of ameba
ameba
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Broadbent:
In your code, you have nested On Error Goto's.  There can only be one active error handler at a time.  An error handler is active until the Exit Sub, End Sub, or Resume statement is executed. You posted:

err
err.clear
on error goto oleerr
createobject("whatever")
exit sub

If you ever get into err, then, even though you cleared the current error, the error handler is still active for the last error and will not be able to handle an error for the next CreateObject statement within the error procedure.

Your error handler should either Exit the sub or Resume the next statement in code.
Thanks it was helpful, but the real clue came from Erick37. Thanks again.

My telephone line has been down - hence the delay.
Thanks for B.