<html>
<body>
At 09:38 AM 5/12/2003 +0100, Anthony Evershed wrote:<br>
<blockquote type=cite class=cite cite>I'm having a little trouble using
Python's Win32com library to automate<br>
Microsoft Word 2000. I'm trying to highlight (block mark) a section
of<br>
text from a known starting point to the end of the same line, ready
for<br>
copying or deletion.<br><br>
For example, let's say that I want to select all the text after 
(and<br>
including) the word &quot;highlight&quot;, in the line above. I can get
to the<br>
start of the selection by saying:<br><br>
finder = wd.Selection.Find<br>
finder.Text = &quot;highlight&quot;<br>
finder.Execute()<br>
wd.Selection.MoveLeft()<br><br>
and from there I can get to the end of the line using<br><br>
wd.Selection.EndKey()<br><br>
I can see that the EndKey command allows for extra parameters - the<br>
tooltip displayed for it says<br><br>
(Unit=&lt;PyOleMissing object at 0x0205B010&gt;, Extend=&lt;PyOleMissing
object<br>
at 0x0205B010&gt;)<br><br>
The &quot;Extend=&quot; parameter looks as if it should do the job,
however the<br>
fact that it says &lt;PyOleMissing object at 0x0205B010&gt; worries
me<br>
slightly, </blockquote><br>
Ignore that.<br><br>
<blockquote type=cite class=cite cite>especially as<br><br>
wd.Selection.EndKey(1,1)<br>
wd.Selection.EndKey(Unit=1, Extend=1)<br>
wd.Selection.EndKey(1)<br>
wd.Selection.EndKey(0)<br><br>
all raise the exception<br><br>
Traceback (most recent call last):<br>
&nbsp; File &quot;&lt;interactive input&gt;&quot;, line 1, in ?<br>
&nbsp; File<br>
&quot;C:\Python22\lib\site-packages\win32com\gen_py\00020905-0000-0000-C000-0<br>
00000000046x0x8x1.py&quot;, line 10178, in EndKey<br>
&nbsp;&nbsp;&nbsp; return self._oleobj_.InvokeTypes(0x1f9, LCID, 1, (3,
0), ((16396,<br>
17), (16396, 17)),Unit, Extend)<br>
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft
Word',<br>
'Bad parameter', 'C:\\Program Files\\Microsoft<br>
Office\\Office\\1033\\wdmain9.chm', 36888, -2146824168),
None)</blockquote><br>
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 &quot;how did he figure <i>that </i>out&quot;? Easy,
and you can do it too! In Word, open the Visual Basic Editor.
(Tools-&gt;Macros-&gt;). From there open the Object Browser
(View-&gt;).<br>
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:<br><br>
<b><i>Unit</i></b>&nbsp;&nbsp; Optional <b>Variant</b>. The unit by which
the selection is to be moved or extended. Can be one of the following
<b>WdUnits</b> constants: <b>wdStory</b>, <b>wdColumn</b>, <b>wdLine</b>,
or <b>wdRow</b>. The default value is <b>wdLine</b>.<br><br>
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.<br><br>
You can also use the immediate window to test things.<br><br>
I suggest getting O'Reilly's <i>Learning Word Programming
</i>book.<br><br>
<blockquote type=cite class=cite cite>Secondly, is there any way to use
text (or rather the ASCII equivalent)<br>
selected in this way in Python (that is, getting the text out of
Word<br>
into a Python string)?</blockquote><br>
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
&quot;highlight&quot;. <br><br>
print wd.Selection.Text will print &quot;using&quot;<br>
wd.Selection.Text = 'foo' will replace &quot;using&quot; with
&quot;foo&quot;<br><br>
[snip]<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>