how to handle nonexistent class?

·

·s¤@¥N½a¤H

I write a class in VB and plan to use it in word.

under word vba:

set obj = new myproject.class1

If the PC does not install my dll, error will occur.

What I do in Word VBA to check the exist of the class?


thanks
 
J

Jay Taplin

Well, there are probably a couple of ways to handle this. As I don't
know any code to check if a referenced DLL is actually loaded in memory
in VBA, I'll tell you the late-binding method.

Dim obj As Object

On Error Goto EH_ClassNotFound

Set obj = CreateObject("MyProject.Class1")

'Class was instantiated correctly, therefore things should be okay
'Place code here

Exit Sub 'Or Function, or Property
EH_ClassNotFound:
MsgBox "MyProject DLL not loaded!", vbCritical or vbOKOnly

What is being done here is an object is created. The object can be
instatiated as basically anything type of object. Use CreateObject to
dynamically get an instance of an object; if it fails, you'll get an
error 429, which is been trapped by using the On Error Goto
EH_ClassNotFound (you could go even further and make sure the error
number really is 429, but we won't go that far). If you don't get an
error, all is well and you can proceed with your code.

Good luck.

Jay Taplin MCP
 
?

?????

Really thanks for your information. I can solve the problem now.

But can you tell me more about the different between

Set obj = CreateObject("MyProject.Class1")

and

Set obj=new myproject.class1


Is there any different?

Thanks ~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top