Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error POINTER(IUnknown) when trying to access COM object properties

This code in Python is not working:

from comtypes import client
word = client.CreateObject("Word.Application")
word.Documents.Open("C:\\test.docx")

I get this error message:

Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
doc = word.Documents.Open("test.docx")
AttributeError: 'POINTER(IUnknown)' object has no attribute 'Documents'

Similar code using the class Excel.Appliation also gives the same kind of error. Another test with InternetExplorer.Application worked. So it seems a problem of Office.

I also tested the same code in VBScript and worked.

The code works in Windows7-64bits and Office 2013. The computer where it is not working is Windows10-64bits and Office 365. The Python version in both computers is 3.6-64bits.

like image 571
Tripanosomagambiense Avatar asked Jul 29 '26 11:07

Tripanosomagambiense


1 Answers

This is not a good solution but I think I will be able to live with it. I'm using this code now:

from comtypes import client
word = client.CreateObject("Word.Application", dynamic = True)
word.Visible = True
word.Documents.Open("test.docx")
word.Documents[0].SaveAs("test.pdf", 17)
word.Documents[0].Close()

Important is the word.Visible = True row and the dynamic = True parameter. Without them the Documents collection is not found.

like image 200
Tripanosomagambiense Avatar answered Aug 01 '26 09:08

Tripanosomagambiense