I have used pythonnet to instantiate MS Access. Once the instance is created, I intended to open a file using OpenCurrentDatabase but I subsequently discovered that a python "object" is converted to a "System.object" by pythonnet. My code is (mostly thanks to Gord Thompson):
Code
import clr
clr.AddReference('System')
t = System.Type.GetTypeFromProgID('Access.Application')
access = System.Activator.CreateInstance(t)
print(access)
access.OpenCurrentDatabase(r'C:\Users\Test.accdb')
access.Visible = True
num = 1
rtrn_val = access.Run('TestLink', num)
print(rtrn_val)
The code successfully instantiates MS Access but I am then unable to manipulate the object in what I would have thought was the normal way owing (I think) to the "object" to "System.__ComObject" conversion that occurs and can be seen in the error trace.
Error trace
>>> import clr
>>> clr.AddReference('System')
<System.Reflection.RuntimeAssembly object at 0x0000000004163CC0>
>>> t = System.Type.GetTypeFromProgID('Access.Application')
>>> access = System.Activator.CreateInstance(t)
>>> print(access)
System.__ComObject
>>> access.OpenCurrentDatabase(r'C:\Users\Test.accdb')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '__ComObject' object has no attribute 'OpenCurrentDatabase'
My end game is to do the following:
In Python:
- download information from a website (mostly using selenium)
- send the information from the website to MSAccess by calling a VBA function from Python
In Access:
- after being initiated by Python, run a bunch of routines to process the downloaded data and insert into tables
- return a value to the Python code that originally called the VBA function
I am hoping that there is a simple explanation that someone can give me to advise how I can reference the System.__ComObject.something.OpenCurrentDatabase and System.__ComObject.something.Run(Access VBA code, args). I appreciate anyone's help on this.
- I would prefer to not use IronPython as I need to make use of other Python modules (eg Selenium) which I have not been able to load in IronPython due to pip problems with IronPython (and maybe they're not compatible with IronPython)
- should I have to use IronPython I will need to call functions between CPython and IronPython and after much reading on the web I am still unsure how this can be achieved