I've copied the example code https://inneka.com/programming/vba/moving-numpy-arrays-from-vba-to-python-and-back/ (solution 2) to a VBA Sub and a python script.
import sys, os, win32api, win32com.server.localserver, win32com.server.register
import numpy as np
class MyModule(object):
_reg_clsid_ = "{5B4A4174-EE23-4B70-99F9-E57958CFE3DF}"
_reg_desc_ = "My Python COM Server"
_reg_progid_ = "Python.MyModule"
_public_methods_ = ['MyFunction']
def MyFunction(self, data) :
arr = np.array(data) + 1
return arr
def register(*classes) :
regsz = lambda key, val: win32api.RegSetValue(-2147483647, key, 1, val)
python_path = 'C:/Program Files (x86)/Python/Python37-32/python.exe'
server_path = 'C:/Program Files (x86)/Python/Python37-32/Lib/site-packages/win32com/server/localserver.py'
for cls in classes :
file_path = sys.modules[cls.__module__].__file__
class_name = '%s.%s' % (os.path.splitext(os.path.basename(file_path))[0], cls.__name__)
command = '"%s" "%s" %s' % (python_path, server_path, cls._reg_clsid_)
regsz("SOFTWARE\\Classes\\" + cls._reg_progid_ + '\\CLSID', cls._reg_clsid_)
regsz("SOFTWARE\\Classes\\AppID\\" + cls._reg_clsid_, cls._reg_progid_)
regsz("SOFTWARE\\Classes\\CLSID\\" + cls._reg_clsid_, cls._reg_desc_)
regsz("SOFTWARE\\Classes\\CLSID\\" + cls._reg_clsid_ + '\\LocalServer32', command)
regsz("SOFTWARE\\Classes\\CLSID\\" + cls._reg_clsid_ + '\\ProgID', cls._reg_progid_)
regsz("SOFTWARE\\Classes\\CLSID\\" + cls._reg_clsid_ + '\\PythonCOM', class_name)
regsz("SOFTWARE\\Classes\\CLSID\\" + cls._reg_clsid_ + '\\PythonCOMPath', os.path.dirname(file_path))
regsz("SOFTWARE\\Classes\\CLSID\\" + cls._reg_clsid_ + '\\Debugging', "0")
if __name__ == "__main__":
register(MyModule)
Edit: Added python_path and server_path manually.
Initializing the com server works fine (just run the python script). Also runing a simple function that returns 1 works. When i import numpy it crashes in vba and results in this error message:
Error # -2147467259 was generated by Python COM Server Internal Error
Error Line: 0
Unexpected Python Error: Traceback (most recent call last):
File "C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages\win32com\server\policy.py", line 136, in CreateInstance
return retObj._CreateInstance_(clsid, reqIID)
File "C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages\win32com\server\policy.py", line 194, in _CreateInstance_
myob = call_func(classSpec)
File "C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages\win32com\server\policy.py", line 728, in call_func
return resolve_func(spec)(*args)
File "C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages\win32com\server\policy.py", line 717, in resolve_func
module = _import_module(mname)
File "C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages\win32com\server\policy.py", line 736, in _import_module
__import__(mname)
File "C:/Users/*user*/python/19-10-29_VBA_python\myserver.py", line 4, in <module>
import numpy as np
File "C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
from . import _mklinit
ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.
Edit: Don't know why it takes this path (this is a 64bit application) and not the defined one: C:\Users\*user*\AppData\Local\Continuum\anaconda3\envs\pyApp\lib\site-packages