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.
LVL 4
broadbentAsked:
Who is Participating?
 
amebaConnect With a Mentor Commented:
That means that there is no error generated.

    Set oWDBasic = CreateObject("Word.Basic")
    If oWDBasic Is Nothing Then
        For i = 1 To 200
            DoEvents
        Next
        ' retry
        Set oWDBasic = CreateObject("Word.Basic")
    End If
    If oWDBasic Is Nothing Then
        On Error Go To 0
        Err.Raise vbObjectError + 1021, "Cannot create object", App.Name & "ModuleName.MyOCXSub"
    End If
0
 
amebaCommented:
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
0
 
troywillmotCommented:
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.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
broadbentAuthor Commented:
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
0
 
Erick37Commented:
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.
0
 
broadbentAuthor Commented:
Thanks it was helpful, but the real clue came from Erick37. Thanks again.

My telephone line has been down - hence the delay.
0
 
amebaCommented:
Thanks for B.
0
All Courses

From novice to tech pro — start learning today.