[python-win32] Pythonwin - Word automation - Removing watermark not working

John Machin sjmachin at lexicon.net
Sun Nov 6 01:25:18 CET 2005


Gregory Piñero wrote:
> See below:
> 
> On 11/5/05, *John Machin* <sjmachin at lexicon.net 
> <mailto:sjmachin at lexicon.net>> wrote:
> 
>     Gregory Piñero wrote:
> 
>      >
>      > Would anyone happen to know why this my function removewatermark() in
>      > this code isn't working?  I copied it from a Word macro I
>     recorded and
>      > it did work when I recorded the macro.  When I run it the watermark
>      > doesn't go away.
>      >
>      > I've also attached the code in case the formatting gets messed up
>     from
>      > the email.
>      >
>      >
>      > <code>
>      > import sys
>      > import os
>      > from win32com.client import gencache, constants
> 
>     Idle curiosity: why import constants but not use it?
> 
> 
> It was giving me errors when I tried to use the constants so I stopped 
> using them.  A future question perhaps...
> 
>      > WORD='Word.Application'
>      > False,True=0,-1
>      >
>      > class Word:
>      >     def __init__(self):
>      >         self.app=gencache.EnsureDispatch(WORD)
>      >         self.app.Visible = 1
>      >         self.app.DisplayAlerts = 0
> 
>     What does this do? Suppress any warnings?
> 
> 
> That is correct.

Try unsuppressing warnings. You may learn something.


>  
> 
>      >     def open(self,doc):
>      >         self.app.Documents.Open(FileName=doc)
>      >     def removewatermark(self):
>      >         self.app.ActiveDocument.Sections(1).Range.Select()
>      >         self.app.ActiveWindow.ActivePane.View.SeekView = 9 #
>      > wdSeekCurrentPageHeader
> 
>     ... the cause of the "idle curiosity" question. 
> 
> 
> This doesn't bother me much to write out numbers.

It mightn't bother you. Seeing hard-coded magic numbers bothers the 
bejasus out of many people.

> 
>      >
>      >
>     self.app.Selection.HeaderFooter.Shapes("PowerPlusWaterMarkObject1").Select()
> 
> 
>     Have you tried inspecting self.app.Selection before and after the
>     attempt to .Delete() it?
> 
> 
> What do you mean by inspect?

It is an object. It must have some properties that you can retrieve and 
display so that you have some reassurance that you've actually got the 
object you think you've got.



> 
>      >         self.app.Selection.Delete()

What happens if you try foo.Delete() and foo is 
frozen/write-protected/whatever?


>      >         self.app.ActiveWindow.ActivePane.View.SeekView = 0
>      > #wdSeekMainDocument
>      > </code>
>      >
> 
>     How do you know it didn't work?
> 
> 
> Well I run my code, word opens the file, other changes happen, but the 
> watermark doesn't dissapear.
> 
>     Your class and its methods don't have any problems that I can see --
>     but it's early here and I'm not quite awake. Is it possible for you to
>     show us the few lines of code that you actually used to test this? You
>     know, something like:
> 
>     wc = Word()
>     wc.open("myfile.doc")
>     wc.removewatermark()
>     # then what?
> 
> 
> Something like this:
> 
> word=Word()
> word.open("myfile.doc")
> word.replace("[Sender]","Bill Brown") #replace a word
> word.removewatermark()
> word.printdoc(numcopies=3) #prints, works fine
> word.close() #closes doc
> word.quit() #shuts down word
>  
> I only included the relevant parts of this class. So you see some 
> methods above that aren't in my class.  I guess I should have included 
> the whole class, sorry.  Now I can't get back to that code until Tuesday :-(
> But all of the methods work fine except for removewatermark.
> 

We're not interested in the parts of your class that are irrelevant to 
the problem. If by Tuesday, no-one has been able to help you by 
inspecting the code that you have shown so far, considering doing this:

1. Make up a *small* .doc file with your watermark in it. Make it from 
scratch i.e. don't copy an existing file and delete your proprietary 
info -- it may not actually be deleted.
2. Assemble the *minimal* Python script that demonstrates the problem.
3. Send it to this mailing list. Include in your message which version 
of (a) Python (2) win32com (3) MS Word (4) MS Windows you are using.

HTH,
John


More information about the Python-win32 mailing list