[python-win32] Selecting text from Microsoft Word 2000
usingwin32all
Graham Bloice
graham.bloice at trihedral.com
Mon May 12 18:26:16 EDT 2003
> 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, especially as
>
> wd.Selection.EndKey(1,1)
> wd.Selection.EndKey(Unit=1, Extend=1)
> wd.Selection.EndKey(1)
> wd.Selection.EndKey(0)
>
It would seem that you haven't yet run MakePy, hence the PyOleMissing. Run
this on the Word type library, and you can then use the correct constants
via win32com.client.constants in the EndKey call.
The Unit parameter can have values of wdStory, wdColumn, wdLine and wdRow.
The default is wdLine.
The Extend parameter can have values of wdMove and wdExtend. The default is
wdMove.
In your case, to extend the selection to the end of the line use:
wd.Selection.EndKey(Extend=win32com.client.constants.wdExtend)
I don't think that you need the intervening wd.Selection.MoveLeft() either.
Graham Bloice
More information about the Python-win32
mailing list