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?

2
  • I see you are new to the site, so I take the liberty to suggest to mark my solution as accepted if it works for you. :-) Commented Oct 24, 2008 at 9:24
  • The link is not working. Do you have an updated one? Commented Jun 18, 2013 at 19:25

2 Answers 2

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! ;-)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.