By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Problems with Python for Windows extensions

P: n/a
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?

Sep 3 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"KK" <Ra*****@gmail.com> schrieb im Newsbeitrag
news:11*********************@z14g2000cwz.googlegro ups.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

Sep 4 '05 #2

P: n/a
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...

Sep 4 '05 #3

P: n/a
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:
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?

Sep 4 '05 #4

P: n/a
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

Sep 7 '05 #5

P: n/a
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
Sep 7 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.