[python-win32] need help for searching of text in a textbox for ms word
Tim Golden
mail at timgolden.me.uk
Thu Jan 17 09:49:52 CET 2008
[Copying back to the list... again. Please keep
your posts on-list: I'm not the only person who
can answer questions about python-win32!]
a_n_s_h_a_n wrote:
> hi! i found something on the web about finding
> and replacing text anywhere in the ms word document.
> the problem is that it is in vb form. i need help
> converting this to python specifically the last line.
> i tried converting the last line to
> "Selection.Find.Execute(Replace=wdReplaceAll)" but python
> said that wdReplaceAll is not defined. please help
> converting. thanks
> Find.Text = <find text>
> Find.Replacement.Text = <replacement text>
> Selection.Find.Execute Replace:=wdReplaceAll
OK. Good effort. As you've realised, while VBA/S allows
function calls without brackets, Python does not, so you're
quite right to use .Execute (Replace...
The missing piece is that the constants like wdReplaceAll
which are available in the global namespace under VBA/S
are in a special constants namespace under Python, and
even then only if you've generated the proxy modules
(early-binding) for the COM Object.
A suitable code snippet would be:
<code>
import win32com.client
#
# Make sure the proxy module is generated
#
word = win32com.client.gencache.EnsureDispatch ("Word.Application")
constants = win32com.client.constants
print constants.wdReplaceAll
#
# ... other bits of program
#
Selection.Find.Execute (Replace=constants.wdReplaceAll)
</code>
I haven't actually tested the .Find.Execute bit (and I'm
not really that conversant in Word automation) but the
rest is sound enough.
TJG
More information about the python-win32
mailing list