[python-win32] MS Word and Python - how to disable SaveAs event?

bob gailer bgailer at alum.rpi.edu
Sat Dec 15 04:04:00 CET 2007


Wojtek P wrote:
> 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 <http://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
The reason Cancel = True is not working is that arguments to functions 
are passed by value, and seen as local variables. This assignment 
replaces the local Cancel; it does not affect the caller. I wonder if it 
might work to return False?
>  
>   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()
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>   



More information about the python-win32 mailing list