[python-win32] MS Word and Python - how to disable SaveAs event?
Wojtek P
wojtulek at gmail.com
Fri Dec 14 09:26:00 CET 2007
Hello!
I try use Microsoft Word and OpenOffice with Python - and i have some
problems. How to disable "Save As" in word from python code, or how to
cancel this function when user use it? This code below doesn't work :/
wxPython 2.8.4.2
Python 2.4.4
from win32com.client import DispatchWithEvents, Dispatch
import msvcrt, pythoncom
import time, sys
import types
import os
import threading
import wx
stopEvent = threading.Event()
##############################
#################################################
class WordEvents:
###############################################################################
def OnDocumentChange(self):
self.seen_events["OnDocumentChange"] = None
print "OnDocumentChange"
def OnWindowActivate(self, doc, wn):
self.seen_events["OnWindowActivate"] = None
print "OnWindowActivate"
def OnQuit(self):
print "OnQuit"
self.seen_events["OnQuit"] = None
stopEvent.set()
def OnStartup(self):
print "OnStartup"
self.seen_events["OnStartup"] = None
def OnDocumentBeforeSave(self, Doc, SaveAsUI, Cancel):
print "OnDocumentBeforeSave"
print "SaveAsUI:", SaveAsUI
if SaveAsUI:
Cancel = True
return
self.seen_events["OnDocumentBeforeSave"] = None
def OnDocumentBeforeClose(self):
print "OnDocumentBeforeClose"
self.seen_events["OnDocumentBeforeClose"] = None
###############################################################################
class MyWord:
###############################################################################
def __init__(self):
self.wordApp = None
self.wordDoc = None
def __del__(self):
pythoncom.CoUninitialize()
def Init(self):
self.wordApp = DispatchWithEvents('Word.Application', WordEvents)
self.wordApp.seen_events = {}
self.wordApp.Visible = 0
self.wordDoc = self.wordApp.Documents.Add()
def Start(self):
print "Start..."
s = self.wordDoc.Sentences(1)
s.Text = 'Hello world !'
self.wordDoc.Saved = 1
self.wordApp.Visible = 1
def WaitForFinish(self):
while True:
pythoncom.PumpWaitingMessages()
try:
if not self.wordApp.Visible: # jesli zamkniecie worda to czas wyjsc
return 0
except pythoncom.com_error:
pass
return 1
def Wait(self):
return self.WaitForFinish()
def Stop(self):
self.wordApp.Quit()
###############################################################################
word_ed = MyWord()
word_ed.Init()
word_ed.Start()
word_ed.Wait()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20071214/e527879f/attachment.htm
More information about the python-win32
mailing list