I need to change some custom properties values in many files. Here is an example of code - how I do it for a single file:
import win32com.client
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False
doc = MSWord.Documents.Open(file)
doc.CustomDocumentProperties('Some Property').Value = 'Some New Value'
doc.Save()
doc.Close()
MSWord.Quit()
Running the same code for "Excel.Application"
(with minor changes - just to make it work) gives me excellent result. However when I'm using doc.Save()
or doc.SaveAs(same_file)
for MSWord it silently fails. I don't know why, but changes are not saved.
Now my workaround is to use SaveAs
to a different file, it also works good. But I want to understand why I have such strange behaviour for MSWord files and how it can be fixed?
Edit: I changed my code, not to misdirect people with silent fail cause of try/except. However, thanks to all of them for finding that defect in my code :)