Where is Word - COM solution

Guy Lateur guy.lateur at b-b.be
Thu Jun 23 03:49:10 EDT 2005


Hi all,

This goes back to my previous post called "Where is Word". In short, I 
wanted to make a temporary file (directory listing), open it in Word to let 
the user edit, layout and print it, and then delete the temp file 
afterwards.

I almost got it to work without using COM, but there was a problem when the 
user runs the script a second time without closing Word after the first 
time. In that case, the temp file could not be deleted (see original post).

Luckily, the following code was provided to me by a good fellow named 
Hughes, Chad O. Thanks again for your time and effort on this, Chad!


<code>
import os
from win32com.client import Dispatch

dirlist = os.listdir(os.getcwd())

word = Dispatch('Word.Application')
word.Documents.Add(Visible = True)
word.Visible = True

for line in dirlist:
  word.Selection.TypeText(line + '\r\n')
</code>


So the questions 'where is Word' and 'how to delete the temp file' have 
become obsolete. Nice, eh? COM rules!

Cheers,
g









More information about the Python-list mailing list