2

I have a simple python class that I am trying to make com-accessible (e.g., to VBA):

class Foo(object):
    _reg_progid_ = 'Foo.Application'
    _reg_clsid_ = '{602462c5-e750-4d1c-879b-a0465bebb526}'
    _public_methods_ = ['say_hello']

    def __init__(self):
        pass

    def say_hello(self, name):
        return f'Hello, {name}'


if __name__=='__main__':
    print("Registering COM server")
    import win32com.server.register
    win32com.server.register.UseCommandLine(Foo)

Several examples indicate this is a pretty standard approach (see here and here).

From python, this appears to be com-accessible. No errors raise, and the output appears as expected:

from comtypes.client import CreateObject
f = CreateObject('Foo.Application')
f.say_hello('David')

When trying to instantiate a Foo from VBA, however, there is an error (Run-time error -2147024770 (8007007e) Automation error The specified module could not be found).

Dim f As Object
Set f = CreateObject("Foo.Application")

Run-time error -2147024770 (8007007e) Automation error The specified module could not be found

I am actually able to resolve this error using the method described in this answer (and this one), specifically doing:

_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER

And then localserver.serve('{602462c5-e750-4d1c-879b-a0465bebb526}') in the name guard function.

However, in some past applications development work (a long time ago using python 2.7) I know we did not do this part -- instead we used Innosetup to compile an installer from a foo.exe and foo.dll (derived from foo.py probably from py2exe or similar) and other dependencies.

I'm happy (for now) with the solution, but I guess my question is whether this is necessary (as several examples don't do these things) or if there's something else I'm missing (e.g., the installer that I used in a past life actually handled this bit behind-the-scenes with the DLL instead of a .py file?)?

Additional information: OS is 64-bit Windows, running Excel 2013 (32-bit) and python 3.7.4 (32-bit).

David Zemens
  • 53,033
  • 11
  • 81
  • 130

0 Answers0