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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

amebaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.