[python-win32] Selecting text from Microsoft Word 2000 using win32all

Bob Gailer bgailer at alum.rpi.edu
Mon May 12 11:49:28 EDT 2003


At 09:38 AM 5/12/2003 +0100, Anthony Evershed wrote:
>I'm having a little trouble using Python's Win32com library to automate
>Microsoft Word 2000. I'm trying to highlight (block mark) a section of
>text from a known starting point to the end of the same line, ready for
>copying or deletion.
>
>For example, let's say that I want to select all the text after (and
>including) the word "highlight", in the line above. I can get to the
>start of the selection by saying:
>
>finder = wd.Selection.Find
>finder.Text = "highlight"
>finder.Execute()
>wd.Selection.MoveLeft()
>
>and from there I can get to the end of the line using
>
>wd.Selection.EndKey()
>
>I can see that the EndKey command allows for extra parameters - the
>tooltip displayed for it says
>
>(Unit=<PyOleMissing object at 0x0205B010>, Extend=<PyOleMissing object
>at 0x0205B010>)
>
>The "Extend=" parameter looks as if it should do the job, however the
>fact that it says <PyOleMissing object at 0x0205B010> worries me
>slightly,

Ignore that.

>especially as
>
>wd.Selection.EndKey(1,1)
>wd.Selection.EndKey(Unit=1, Extend=1)
>wd.Selection.EndKey(1)
>wd.Selection.EndKey(0)
>
>all raise the exception
>
>Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File
>"C:\Python22\lib\site-packages\win32com\gen_py\00020905-0000-0000-C000-0
>00000000046x0x8x1.py", line 10178, in EndKey
>     return self._oleobj_.InvokeTypes(0x1f9, LCID, 1, (3, 0), ((16396,
>17), (16396, 17)),Unit, Extend)
>com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word',
>'Bad parameter', 'C:\\Program Files\\Microsoft
>Office\\Office\\1033\\wdmain9.chm', 36888, -2146824168), None)

Where did you get the value 1 for unit? What do you expect it to do? The 
only acceptable values here are 5 (line), 6 (story), 9 (column) and 10 
(row). WOW you say "how did he figure that out"? Easy, and you can do it 
too! In Word, open the Visual Basic Editor. (Tools->Macros->). From there 
open the Object Browser (View->).
In the top comb box select word. In the 2nd combo box type endkey (enter). 
Selection should be boxed in the left pane and EndKey in the right. Click 
EndKey to see the parameter list below. Press F1 to open Help on EndKey. 
Notice:

Unit   Optional Variant. The unit by which the selection is to be moved or 
extended. Can be one of the following WdUnits constants: wdStory, wdColumn, 
wdLine, or wdRow. The default value is wdLine.

Return to the object browser. Scroll on the left down to WdUnits; the named 
constants appear on the right.Click one and you'll see its value below.

You can also use the immediate window to test things.

I suggest getting O'Reilly's Learning Word Programming book.

>Secondly, is there any way to use text (or rather the ASCII equivalent)
>selected in this way in Python (that is, getting the text out of Word
>into a Python string)?

The Selection and Range objects have a Text property. You can read or set 
it. For example, after finder.Execute() the Selection will be the word 
"highlight".

print wd.Selection.Text will print "using"
wd.Selection.Text = 'foo' will replace "using" with "foo"

[snip]

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20030512/544e4b5f/attachment.htm


More information about the Python-win32 mailing list