I am writing a script (a full-fledged program, actually) in PYTHON (version 2.5; the current version is 3.0 but there are some performance problems -- 3.0 is slower than 2.5; go figure). The program allows a user to open a file in WORD. I have tested this program on an XP machine and a VISTA Home Premium machine. With WORD 2003 installed, there is no problem. I call the typical open file box, the user selects a file, WORD comes up (slowly) on the screen and the file displays in WORD. On the machine in my office in Camden where we have WORD 2007, everything happens like before EXCEPT the file does not display in WORD. Indeed, there is an error. The errors are shown in two files attached to this email. Note the difference in exit codes. When I used a global variable to hold the file name, I got an exit code of 1 (bad exit). When I rewrote the file to use a function which passed a local variable, I got an exit code of 0 (which indicates that everything went well) EXCEPT that there was no difference in behavior. The file still did not open in WORD 2007. I'm puzzled to say the least.
I have checked with Microsoft's MSDN and there doesn't seem to be any difference in the COM calls from 2003 to 2007 (I did have to modify my code somewhat to get 2003 to work since I was looking at a book that called WORD 97 and there IS a difference in the name of a WORD method). I also sent a post to the Portable PYTHON list about this but have not yet received a reply. PYTHON doesn't have native support for COM. That comes from an added series of modules which need to be separately downloaded and installed, which I have done. They are functionally the same as modules for the desktop version of PYTHON and, presumably, work the same way, since they were written by the same person. Any ideas?

Thanks for your reply leegeorge07. Please see below...

#
from Tkinter import *
import Pmw
import tkSimpleDialog
import tkFileDialog
from fileProcess import *
from activityLog import *
from win32com.client import Dispatch

root = Tk()
root.title('PDF Converter and FTP Program')
fp = fileProcess()
al = activityLog()

mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X)

def makeFileMenu() :
    fileBtn = Menubutton(mBar,text='File', underline=0)
    fileBtn.pack(side=LEFT, padx="2m")
    fileBtn.menu = Menu(fileBtn)
    
    fileBtn.menu.add_command(label='Open', underline=0, command=open_file)
    fileBtn.menu.add_command(label='Open in WORD', underline=8,command=open_WORD)
    fileBtn.menu.add_command(label='Clear text box', underline=0,command=clear_box)
    fileBtn.menu.add('separator')
    fileBtn.menu.add_command(label="View Activity Log", underline=5,command=viewLog)
    fileBtn.menu.add('separator')
    fileBtn.menu.add_command(label='Quit', underline=0, activebackground='green',command=fileBtn.quit)
    fileBtn['menu'] = fileBtn.menu
    return fileBtn

def open_file():
    fn = tkFileDialog.askopenfilename()
#
# The following line hands the file to open to the fp object.
#
    fp.setFileName(fn)
#
# The following line gets the current file name from the fp object and prints it to the text box.
#
    txtBox.settext(fp.getFileName())
    return fn
#
# Using this widget, the program will NOT display a WORD file.
#
def display_file(fn) :
#
# The following line displays a file in the text box.
    txtBox.importfile(fn)

    
def clear_box() :
    txtBox.clear()
    
def open_WORD():
#
# This may not work with WORD 2008. It DOES work with WORD 2003 on both VISTA and XP. You
# can select a WORD file, WORD is opened, and the file appears in WORD. It does not in WORD 2008
# on the Rowan network. The operating system is XP Professional. I have XP Pro on my laptop along
# with WORD 2003; this program works as it is supposed to.
#
# I wrote an version of open_file in which the function passed a value to fn which was a global variable.
# This program with this version of the function failed and returned an exit code of 1. When I rewrote
# the function with local variables (as it is now), the program failed with an exit code of 0.
#
    fn = open_file()
    wordApp = Dispatch("Word.Application")
    wordApp.Visible = 1
    activity = "\n" + fn + " opened."
    al.addToToday(activity)    
    txtBox.settext(al.getTodayText())
    wordApp.Documents.Open(fn)
#
# This is probably not the best place to write the log to a file. It is here for testing purposes.
#
    al.writeActivityLog()
    
def viewLog():
    alfn = al.getActivityLogFileName()
    display_file(alfn)

fileBtn = makeFileMenu()
txtBox = Pmw.ScrolledText(root, borderframe=1, labelpos=N, label_text='Press Release Activity Log', usehullsize=1, hull_width=400, hull_height=300, text_padx=10, text_pady=10, text_wrap=WORD)
txtBox.pack(fill=BOTH, expand=1, padx=5, pady=5)
root.mainloop()

Editor:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here

[/code]

This time, I did add the needed code tags for you!

This article has been dead for over six months. Start a new discussion instead.