Search code examples
.netvbaexcelcom

Visual Basic - invoking .net runtime


I am trying to invoke objects from .net runtime from VBA (Word/Excel). This works: oT = CreateObject("System.Text.UTF8Encoding") But not this: CreateObject("System.Management.Automation.Runspaces.RunspaceFactory") The error is: Runtime error '429' ; ActiveX component can't create object

What I don't understand is how Excel/Word determines which classes are exposed and which aren't


Solution

  • The argument you specify when invoking CreateObject is not a namespace, it's a ProgId - a string COM uses to locate registered classes (and the library they're defined in, and where that library is located), in the Windows Registry.

    If there's no registered COM type with a ProgId value of System.Management.Automation.Runspaces.RunspaceFactory, then ActiveX can't create that object.

    If there's no COM type library for a namespace (i.e. the assembly isn't registered for COM interop), then you're trying to get COM to understand .NET/managed code, which it can't do.

    What I don't understand is how Excel/Word determines which classes are exposed and which aren't

    Excel/Word/whatever host application has nothing to do with it, it's VBA's own runtime that loads the COM types, and it's the Windows Registry that contains the ProgId for everything that's registered.

    If you search your registry for Scripting.Dictionary under HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID, you'll eventually find a key named {EE09B103-97E0-11CF-978F-00A02463E06F}, with a ProgID subkey having a value of Scripting.Dictionary and an InprocServer32 subkey with a value of C:\Windows\system32\scrrun.dll, and if the VBA runtime succeeds at creating an instance of the type with the specified ProgId in that type library, then the CreateObject call succeeds and returns the instance. Otherwise, it throws an error 429 and says "ActiveX can't create object".