Search & Replace in MS Word Puzzle

Waldemar Osuch waldemar.osuch at gmail.com
Sun Dec 10 15:36:52 EST 2006


I do not have answers for all your questions but a few remarks that may
help.

Ola K wrote:
> Hi guys,
> I wrote a script that works *almost* perfectly, and this lack of
> perfection simply puzzles me.
> I simply cannot point the whys, so any help on it will be appreciated.
> I paste it all here, the string at the beginning explains what it does:
>
> '''A script for MS Word which does the following:
>    1) Assigns all Hebrew italic characters "Italic" character style.
>    2) Assigns all Hebrew bold characters "Bold" character style.
>    2) Assign all English US characters "English Text" (can be regular,
> Italic, or Bold)
>

-- Code snipped

> So generally speaking this script works quite nicely, BUT:
>
> 1. Despite this sort of conditions:
> " if "Italic" not in doc.Styles: "
> if this style already exists I get an error, and the program stops.
> Any idea how can that be?...

doc.Styles is a container (a build in Word object) holding instances of
Styles
(another build in Word object).  One way to make the intended check
would be.
style_names = set(s.NameLocal for s in doc.Styles)
if "Italic" not in style_names:
   # create style

>
> 2. The replacement of the English characters doesn't seem to work very
> well. It either assigns all English characters "English Text Bold", or
> "English Text Italic" and with no apparent reason.
> ?....

Read about Range object in Word VBA documentation.  Range.Collapse may
explain what happens here.

>
> 3. The command
> " word.Visible=1 "
> doesn't work anymore. I say "anymore" because it used to work, but
> later I ran "COM Makepy Utility" on "Microsoft Word 10 Object Library
> (8.2)" and since then it stopped working. On Excel, for example, I
> never ran Makepy and this commands works fine for it.
> Any idea on this one?...

This should work.  If word.visible = True used to work but stopped
after running Makepy it could be explained.  Looks to me there are some
other factors in play.

>
> 4. In the end of this long weekend I was pretty satisfied with my
> script (even if not fully functioning) and used PY2EXE to make it an
> .exe file so that I can use it in my work place. But alas, that .exe
> file does not work because it doesn't recognize any of win32com client
> constants. Such as "wdStyleTypeCharacter", or "wdEnglishUS"
> How can I solve this one?

This is described in py2exe wiki.
http://www.py2exe.org/index.cgi/IncludingTypelibs

Waldemar




More information about the Python-list mailing list