This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Creating objects with python

Former Member
Former Member
Hello all,
Does anyone know why I can't create objects using python's win32com to interact with MFiles - desktop version. I managed to create program that can reed info from MFiles (properties and documents), but I can't create objects.
Error that I keep getting is:
  File "C:\Users\markof\AppData\Local\Temp\gen_py\3.8\B9C079AA-92DD-4FB4-A0E0-AA3198955B45x0x1x0.py", line 17604, in CreateNewObject
    ret = self._oleobj_.InvokeTypes(5, LCID, 1, (9, 0), ((3, 1), (9, 1), (9, 49), (9, 49)),ObjectType
TypeError: The Python instance can not be converted to a COM object

import win32com.client

mfiles = win32com.client.DispatchEx("MFilesAPI.MFilesClientApplication")
vault_connection = mfiles.GetVaultConnection("MD Vault")
m_vault = vault_connection.BindToVault("00000", True, True)

#Create property definitions
oPropertyValues = win32com.client.DispatchEx("MFilesAPI.PropertyValues")

# Add 'Name and Title' property by creating a new PropertyValue object.
# The PropertyValue object encapsulates the metadata information (PropertyDef)
# and the actual value (TypedValue).
oPropertyValue1 = win32com.client.DispatchEx("MFilesAPI.PropertyValue")

oPropertyValue1.PropertyDef = 0
oPropertyValue1.TypedValue.SetValue(1, "DemoObject")
oPropertyValues.Add(0, oPropertyValue1)

# Add 'Class' property
oPropertyValue2 = win32com.client.DispatchEx("MFilesAPI.PropertyValue")
oPropertyValue2.PropertyDef = 100
oPropertyValue2.TypedValue.SetValue(9,1)
oPropertyValues.Add(1, oPropertyValue2)

# Add 'Single file' property
oPropertyValue3 = win32com.client.DispatchEx("MFilesAPI.PropertyValue")
oPropertyValue3.PropertyDef = 22
oPropertyValue3.TypedValue.SetValue(8, True)
oPropertyValues.Add(2, oPropertyValue3)

# Add source file.
oSourceFiles = win32com.client.DispatchEx("MFilesAPI.SourceObjectFiles")
oSourceFile1 = win32com.client.DispatchEx("MFilesAPI.SourceObjectFile")
oSourceFile1.SourceFilePath = "test.txt"
oSourceFile1.Title = "test"
oSourceFile1.Extension = "txt"
oSourceFiles.Add(0, oSourceFile1)

#  Create new object.

oObjectVersionAndProperties = m_vault.ObjectOperations.CreateNewObject(0, oPropertyValues, oSourceFiles)
  • Former Member
    Former Member
    I think this only works for mfiles web service. My company uses Windows instalation, so I cant interact with REST API, I need to use COM API.
  • Former Member
    Former Member
    Just to inform you, if someone wants to interact with python and Mfiles with COM. I solved this problem by using comtypes instead win32com.
    You can check code below:

    from comtypes.client import CreateObject

    mfiles = CreateObject("MFilesAPI.MFilesClientApplication")
    m_vault = mfiles.BindToVault("test", 00000, True, True)
    oPropertyValues = CreateObject("MFilesAPI.PropertyValues")

    oPropertyValue1 = CreateObject("MFilesAPI.PropertyValue")
    oPropertyValue1.PropertyDef = 0
    oPropertyValue1.TypedValue.SetValue(1, "DemoObject")
    oPropertyValues.Add(0, oPropertyValue1)

    oPropertyValue2 = CreateObject("MFilesAPI.PropertyValue")
    oPropertyValue2.PropertyDef = 100
    oPropertyValue2.TypedValue.SetValue(9, 1)
    oPropertyValues.Add(1, oPropertyValue2)

    oObjectVersionAndProperties = m_vault.ObjectOperations.CreateNewObjectEx(129, oPropertyValues)