Problems with Python for Windows extensions

K

KK

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?
 
V

vincent wehren

| 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"
 
K

KK

thx for ur reply
u r rite that i should use a raw string, but that doesn't solve the
problem
i am q annoyed by this strange behaviour.
i tried to run the script on my friend's pc, which is python2.4 + pywin
204 + office 2000, but same thing happened

now i am thinking to generate a vbs from python and run it. i know it
is dumb but i dont know other solutions...
 
K

Kartic

Hi,

Invoking Execute as shown below works.. I have no explanation why your
VB to Py converted code did not work.

wdFindContinue = 1
objSelection.Find.Execute('Contosa', False, True, False, False, True,
True, wdFindContinue, True, 'Fabrikam', wdReplaceAll, False, False,
False, False)

This finds 'Contosa' and replaces all occurances with 'Fabricam'.

For a full explanation about the arguments to Execute, look it up in the
VBA Help.

Thanks,
-Kartic


The Great 'KK' uttered these words on 9/3/2005 11:14 AM:
 
K

KK

Hello,
I guess you could reproduce my problem, Kartic. I have tried the one u
suggested, but sadly it didn't work for me. I think the COM of pywin is
quite tricky, or it might be a bug. I have some friends who also had
experience of weird behaviors of pywin32, which makes me skeptical of
using it in real app.

Thanks
KK
 
K

Kartic

The Great 'KK' uttered these words on 9/7/2005 7:57 AM:
Hello,
I guess you could reproduce my problem, Kartic. I have tried the one u
suggested, but sadly it didn't work for me. I think the COM of pywin is
quite tricky, or it might be a bug. I have some friends who also had
experience of weird behaviors of pywin32, which makes me skeptical of
using it in real app.

Thanks
KK

Actually, I have created some robust win32com applications (using Word
and Excel) that work consistently on all installed machines.

Could it be an Office 2003 quirk? Did some other app of yours crash
while using COM? Such crashes could produce unpredictable results in COM
related code, from my experience.

So, if you think it is worth your time, you could probably investigate
it a bit more or better contact Mark Hammond to see if he can help. I
don't know if there is a win32com mailing list; if there is one, please
post your question there too.

Thanks,
-Kartic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top