Problems with Python for Windows extensions

vincent wehren vincent at visualtrans.de
Sun Sep 4 02:53:26 EDT 2005


"KK" <RayFLau at gmail.com> schrieb im Newsbeitrag 
news:1125760475.488680.17070 at z14g2000cwz.googlegroups.com...
| the code below is taken from M$ technet as an example on using vb
| script to do a replace all in word:
|
| Const wdReplaceAll  = 2
|
| Set objWord = CreateObject("Word.Application")
| objWord.Visible = True
|
| Set objDoc =
| objWord.Documents.Open("K:\Development\Fabricbase\prod\Test.doc")
| Set objSelection = objWord.Selection
|
| objSelection.Find.Text = "Contoso"
| objSelection.Find.Forward = True
| objSelection.Find.MatchWholeWord = True
|
| objSelection.Find.Replacement.Text = "Fabrikam"
| objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
|
|
|
|
| I did a rewrite and made it pythonic:
|
| from win32com.client import *
|
| wdReplaceAll  = 2
|
| objWord = Dispatch("Word.Application")
| objWord.Visible = True
|
| objDoc =
| objWord.Documents.Open("K:\Development\Fabricbase\prod\Test.doc")
| objSelection = objWord.Selection
|
| objSelection.Find.Text = "Contoso"
| objSelection.Find.Forward = True
| objSelection.Find.MatchWholeWord = True
|
| objSelection.Find.Replacement.Text = "Fabrikam"
| objSelection.Find.Execute (Replace = wdReplaceAll)
|
|
| However, the document juz loaded up in word but no action was taken. I
| am using Word 2003. Any ideas?

KK,

Your example seemed to work fine for me (Python2.4, Pythonwin build 204, 
Word 2003)

One thing: since you say your document loads up fine I don't know if it is 
just a typo, but make sure you follow the rules of backslash literals in 
path names. In other words:
"K:\Development\Fabricbase\prod\Test.doc" should read either

r"K:\Development\Fabricbase\prod\Test.doc" (note the leading r for raw 
string
or

"K:\\Development\\Fabricbase\\prod\\Test.doc"
or

"K:/Development/Fabricbase/prod/Test.doc"

--

Vincent Wehren








More information about the Python-list mailing list