0

I want to override the default CreateObject() function in VBScript with my own.

Basically this example in VB6:

http://www.darinhiggins.com/the-vb6-createobject-function/

I cannot figure out is this line:

 Set CreateObject = VBA.CreateObject(Class$, ServerName$)

How do I refer to "VBA" in VBSript?

Community
  • 1
  • 1

2 Answers2

4

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
  • 38,673
  • 6
  • 89
  • 128
0

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
  • 3,770
  • 19
  • 29