Problem exiting a Word document using Python

This is my first time using this, so be kind :) Basically, my question is that I create a program that opens many Microsoft Word 2007 documents and reads from a specific table in this document and writes this information to an excel file . as well as over 1000 words. It all works for me, but the only problem when I run my code, it does not close MS Word after opening each document. I need to do this manually at the end of the program by running a word and selecting the exit word option from the main menu. Another problem is that if you run this program sequentially in the second run, everything goes to hell, it prints the same thing repeatedly, regardless of which document is selected. I think this may be related to how MSword decides which document is active, for example.it still opens the last active document that has not been closed since the last run. Anyways here is my code to open and close the part, I won’t miss you guys with the rest:

MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0
# Open a specific file
#myWordDoc = tkFileDialog.askopenfilename()
MSWord.Documents.Open("C:\\Documents and Settings\\fdosier" + chosen_doc)
#Get the textual content
docText = MSWord.Documents[0].Content
charText = MSWord.Documents[0].Characters
# Get a list of tables
ListTables = MSWord.Documents[0].Tables

------Main Code---------

MSWord.Documents.Close
MSWord.Documents.Quit
del MSWord
+3
3

, Python VBA, :

MSWord.Documents.Close

:

getattr(MSWord.Documents, "Close")

. - . ( :):

MSWord.Documents.Close()

.Quit.

+1

MSWord.Quit :

MSWord.ActiveWindow.Close

MSWord.Quit

, .

+1

I think you need MSWord.Quitat the end (before and / or instead of del)

0
source

All Articles