PDA

View Full Version : Python, Windows and Word


Benny
11-26-2002, 09:01 PM
Hey all,

I am writing a web app to do some processing to a Microsoft Word Mail Merge Document.

In order to do this processing I need to determine what the datafile is of the document. Currently I have these two functions:


def getDataFilename(file):
wd = win32com.client.Dispatch('Word.application')
wd.Documents.Open(file)
doc = wd.ActiveDocument
dataFile = doc.MailMerge.DataSource.Name
doc.Close()
wd.Quit()
return dataFile

def updateDataFilename(file, dataFile):
wd = win32com.client.Dispatch('Word.application')
wd.Documents.Open(file)
doc = wd.ActiveDocument
doc.MailMerge.OpenDataSource(dataFile)
doc.Close()
wd.Quit()


Now, this works if the mail merge document I test it with has been created on the machine that this script is run from.

This is no good, because the whole point of my web application is so people can upload mail merge documents (created on their home pc's) and then have them processed accordingly.

The process flow is as follows:

1. User uploads the mail merge document
2. The datafile is determined
3. User is prompted to upload the datafile - it tells them the location of the file.
4. The mail merge document stored on the server is updated to reflect the new datafile path.
5. The mail merge document is then processed

I've looked everywhere but been unable to find a foolproof way to determine the datafile of ANY Word Mail Merge document.

ANY advice/suggestions, anything would be greatly apprieciated. I'm at a real loss with this one.

Cheers
Ben.

Benny
11-28-2002, 06:31 AM
Well, turns out Word sucks.

Bascially I can't find out the associated datafile of a word document unless the data file is with it.

So what I had to do was write a vbscript that would be executed on the client side, it then passes my script the datafile name.

This was the best option I could come up with .