[python-win32] Selecting text from Microsoft Word 2000 using
win32all
Anthony Evershed
anthony.evershed at npl.co.uk
Mon May 12 10:38:03 EDT 2003
Hi,
I was advised to seek help here for difficulties with Python's Win32
capabilities...
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, 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)
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)?
If it helps, I'm using Python v2.2.2 on Windows NT 4 (though the program
will be running on Windows 2000 machines as well) with win32all build
148.
Many thanks,
Anthony Evershed
anthony.evershed at npl.co.uk
-------------------------------------------------------------------
This e-mail and any attachments may contain confidential and/or
privileged material; it is for the intended addressee(s) only.
If you are not a named addressee, you must not use, retain or
disclose such information.
NPL Management Ltd cannot guarantee that the e-mail or any
attachments are free from viruses.
NPL Management Ltd. Registered in England and Wales. No: 2937881
Registered Office: Teddington, Middlesex, United Kingdom TW11 0LW.
-------------------------------------------------------------------
More information about the Python-win32
mailing list