Hello!<br><br>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 :/
<br><br>wxPython <a href="http://2.8.4.2/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">2.8.4.2</a><br>Python 2.4.4<br><br><br><br>from win32com.client import DispatchWithEvents, Dispatch<br>import msvcrt, pythoncom
<br>import time, sys<br>import types<br>import os <br>
import threading<br>import wx<br>stopEvent = threading.Event()<br><br>##############################<div id="mb_0">#################################################<br>class WordEvents:<br>###############################################################################
<br> def OnDocumentChange(self):<br> self.seen_events["OnDocumentChange"] = None<br> print "OnDocumentChange"<br><br> def OnWindowActivate(self, doc, wn):<br> self.seen_events["OnWindowActivate"] = None
<br> print "OnWindowActivate"<br><br> def OnQuit(self):<br> print "OnQuit"<br> self.seen_events["OnQuit"] = None<br> stopEvent.set()<br><br> def OnStartup(self):<br> print "OnStartup"
<br> self.seen_events["OnStartup"] = None<br><br> def OnDocumentBeforeSave(self, Doc, SaveAsUI, Cancel):<br> print "OnDocumentBeforeSave"<br> print "SaveAsUI:", SaveAsUI<br> if SaveAsUI:
<br> Cancel = True<br> return<br> self.seen_events["OnDocumentBeforeSave"] = None<br> <br> def OnDocumentBeforeClose(self):<br> print "OnDocumentBeforeClose"<br> self.seen_events["OnDocumentBeforeClose"] = None
<br><br>###############################################################################<br>class MyWord:<br>###############################################################################<br> def __init__(self):<br>
self.wordApp
= None<br> self.wordDoc = None<br><br> def __del__(self):<br> pythoncom.CoUninitialize()<br><br> def Init(self):<br> self.wordApp = DispatchWithEvents('Word.Application', WordEvents)<br> self.wordApp.seen_events
= {}<br> self.wordApp.Visible = 0<br> self.wordDoc = self.wordApp.Documents.Add()<br><br> def Start(self):<br> print "Start..."<br> s = self.wordDoc.Sentences(1)<br> s.Text = 'Hello world !'
<br> self.wordDoc.Saved = 1<br> self.wordApp.Visible = 1<br><br> def WaitForFinish(self):<br> while True:<br> pythoncom.PumpWaitingMessages()<br> try:<br> if not self.wordApp.Visible: # jesli zamkniecie worda to czas wyjsc
<br> return 0<br> except pythoncom.com_error:<br> pass<br> return 1<br><br> def Wait(self):<br> return self.WaitForFinish()<br><br> def Stop(self):<br> self.wordApp.Quit()<br><br><br>###############################################################################
<br>word_ed = MyWord()<br>word_ed.Init()<br>word_ed.Start()<br>word_ed.Wait()<br></div>