[python-win32] Welcome to the "python-win32" mailing list (Digest mode)

Mark Hammond skippy.hammond at gmail.com
Tue Nov 8 17:49:10 EST 2022


pythoncom.CLSCTX_LOCAL_SERVER means this COM object will be hosted by 
python.exe - so COM/office is going to look up the registry to get a 
command-line to execute. As written, that will just re-register the COM 
server - so yes, you do want that `localserver.serve()`. The default of 
CLSCTX_INPROC_SERVER generally doesn't need an explicit serve() as 
python.exe isn't used - the DLLs are loaded directly into office - but 
that probably isn't going to help in a concrete way (ie, the underlying 
problem would probably exist in both cases)

Assuming it still fails with that: it might be that this command is 
failing for reasons - eg, if you installed pywin32 via pip and didn't 
run the pywin32_postinstall script I can imagine things going wrong. The 
.exe installer runs that for you though.

You mention you found the entry in the registry - does executing that 
command in a "clean" environment (ie, without any special environment 
variables etc, from a directory unrelated to python - ie, trying to 
emulate the same environment office itself runs in) work OK? If it fails 
importing modules etc, that's probably the same failure when COM tries 
to start it.

You mention "running code as Administrator" - is Office and this 
registration code running as the exact same user?

I've currently no environments with Office available, so can't really 
test at the moment.

HTH,

Mark

On 9/11/2022 2:53 am, Brian Johnson wrote:
> Hi python-win32 community.
> 
> I am trying to get this toy COM server (code is below) to work. It is 
> the sample code from /"Python Programming for Win32"/ and adapted for 
> python 3.10. When I test it using VBA in Word, I get this error:
> Run-time error '429':
> ActiveX component can't create object
> 
>   * I don't know if I am missing one a required _reg_param_ that is
>     needed for Win10.
>   * Is there is an extra step I need to take to register/run the COM server?
>   * I don't know if win32com.server.localserver.serve [] is necessary.
>   * Does the class need an __init__ method to do something?
> 
> 
> I appreciate any help or insight into getting this to work. Thank you!
> Sincerely, Brian
> 
> *Context*:
> 
>   * Windows 10, python 3.10, Office 365, running python code as
>     Administrator
>   * I have verified the python class works in python only.
>   * I tried registering/unregistering using both options shown below.
>     Both appear to work - no error messages. I can find info about the
>     COM Server using Regedit.
>   * I tried the --debug flag, but it didn't provide any verbosity.
>   * I'm learning COM, but the info about it is sparse and generally very
>     abstract.
> 
> 
> *Python script, SimpleCOMServer.py:*
> """A sample COM server - almost as small as they come."""
> # import sys
> 
> importpythoncom
> importwin32com.server.register
> 
> 
> classPythonUtilities:
> """We expose a simple method in a Python COM object."""
> 
> _reg_clsctx_= pythoncom.CLSCTX_LOCAL_SERVER
> _public_methods_= ["SplitString"]
> _reg_progid_= "PythonDemos.Utilities"
> _reg_desc_= "PythonDemos Test COM Server"
> # NEVER copy the following ID
> # Use "print(pythoncom.CreateGUID())" to made a new on.
> _reg_clsid_= "{819E8336-00B5-4025-979A-46EE1EF411B7}"
> 
> # for Python 3.7+
> # 
> https://stackoverflow.com/questions/1054849/consuming-python-com-server-from-net <https://stackoverflow.com/questions/1054849/consuming-python-com-server-from-net>
> _reg_verprogid_= "PythonDemos.Utilities.1"
> # <filename>.<classname>
> _reg_class_spec_= "SimpleCOMServer.PythonUtilities"
> 
> defSplitString(self, val, separator=None):
> """Split a string by a separator."""
> # if separator is not None:
> #     return val.split(separator)
> # else:
> #     return val.split()
> 
> ifseparatorisnotNone:
> returnstr(val).split(str(separator))
> else:
> returnstr(val).split()
> 
> 
> # Add code so that when this script is run by python e.e, it self-registers.
> if__name__== "__main__":
> print("Registering COM server...")
> win32com.server.register.UseCommandLine(PythonUtilities)
> 
> # if "--register" in sys.argv[1:] or "--unregister" in sys.argv[1:]:
> #     win32com.server.register.UseCommandLine(PythonUtilities)
> 
> # else:
> #     # start the server.
> #     from win32com.server.localserver import serve
> 
> #     serve(["{819E8336-00B5-4025-979A-46EE1EF411B7}"])
> 
> *Word VBA macro:*
> Sub TestPython()
>      Dim PythonUtils As Object
>      Set PythonUtils = CreateObject("PythonDemos.Utilities")
>      response = PythonUtils.SplitString("Hello from VB")
>      For Each Item In response
>          MsgBox Item
>      Next
> End Sub
> 
> 
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> https://mail.python.org/mailman/listinfo/python-win32



More information about the python-win32 mailing list