views:

1130

answers:

2

Need help to figure out how to overide the default CreateObject() function in VBScript with my own.

Basically the same exact thing as this example in VB6:

http://www.darinhiggins.com/2007/08/22/TheVB6CreateObjectFunction.aspx

I just need to adapt this to work in VBScript. The only thing I cannot figure out is this line:

VBA.CreateObject(Class$, ServerName$)

How do I refer to "VBA" in VBSript?

any help would be appreciated

A: 

I don't think you can override it so that all code will use it, only YOUR code.

In which case, it doesn't matter what it's called (unless you have tons of existing code you can't change). Can you call it CreateObjectEx() or ExCreateObject() or something like that? Have this function add all your error handling and such and then turn around and call the main/core CreateObject() method

chadmyers
+4  A: 

This quick test seems to work...

Function CreateObject(className, serverName)
   '---- override the CreateObject
   '     function in order to register what
   '     object is being created in any error message
   '     that's generated
   Dim source, descr, errNum

   WScript.echo "In custom CreateObject"
   If Len(serverName) > 0 Then
      Set CreateObject = WScript.CreateObject(className, serverName)
   Else
      Set CreateObject = WScript.CreateObject(className)
   End If

End Function

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject", "")
path = fso.GetAbsolutePathName(".")

WScript.echo path

No guarantees! ;-)

PhiLho