[python-win32] Selecting text from Microsoft Word 2000
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"
>and from there I can get to the end of the line using
>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
>The "Extend=" parameter looks as if it should do the job, however the
>fact that it says <PyOleMissing object at 0x0205B010> worries me
>all raise the exception
>Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
>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.
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
print wd.Selection.Text will print "using"
wd.Selection.Text = 'foo' will replace "using" with "foo"
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-win32