I'm using office automation in Word.
I have a selection and have used Selection.Tables.Add to add a table into
word.

How do I move the selection to the end of the table (i.e. next line?)
If I were to do Selection.TypeText("blah") the text is inserted in the first
cell of the table, which is NOT what I want.

I've tried methods like Selection.Move and MoveEnd... but it doesn't seem to
work. Maybe I'm using it wrong? Any help would be greatly appreciated,

Thank you,
Ray

Re: Selection.Tables.Add -- moving the selection by Jay

Jay
Mon Aug 22 13:48:56 CDT 2005

Ray wrote:
> I'm using office automation in Word.
> I have a selection and have used Selection.Tables.Add to add a table
> into word.
>
> How do I move the selection to the end of the table (i.e. next line?)
> If I were to do Selection.TypeText("blah") the text is inserted in
> the first cell of the table, which is NOT what I want.
>
> I've tried methods like Selection.Move and MoveEnd... but it doesn't
> seem to work. Maybe I'm using it wrong? Any help would be greatly
> appreciated,
>
> Thank you,
> Ray

Hi Ray,

Try this:

Dim oTbl As Table
Set oTbl = ActiveDocument.Tables.Add( _
Range:=Selection.Range, _
NumRows:=2, NumColumns:=2)
oTbl.Select
With Selection
.Collapse wdCollapseEnd
' Selection is now after the end of oTbl
.TypeText "blah"
End With

Personally, I prefer to use the Selection object only for communicating with
the user, and use Range objects for all other work. Here's the equivalent
with a Range object:

Dim oRg As Range
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables.Add( _
Range:=Selection.Range, _
NumRows:=2, NumColumns:=2)
Set oRg = oTbl.Range
With oRg
.Collapse wdCollapseEnd
' oRg is now after the end of oTbl
.Text = "blah"
End With

With this, the text will be after the table, but the cursor will still be in
the first cell of the table. If you want to move it, you can put in
oRg.Select as the last statement of the macro (with or without collapsing
it).

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org



Re: Selection.Tables.Add -- moving the selection by Ray

Ray
Mon Aug 22 23:53:47 CDT 2005

Hello Jay,
Thank you for the info -- it worked great.

Another related question -- when I add the new table, how do I make it keep
the current selection/paragraph indentation? Even if it doesn't keep it, can
you tell me how to properly indent the table? Or at least move the left
anchor position of the first column?

Does anyone have a good reference to Office Automation? My google searches
hasn't been that kind to me lately. Also, I'm coding this in c#, so samples
in that language would be very useful too.

Thanks again,
Ray

"Jay Freedman" <jay.freedman@verizon.net> wrote in message
news:O4LbYp0pFHA.1028@TK2MSFTNGP09.phx.gbl...
> Ray wrote:
>> I'm using office automation in Word.
>> I have a selection and have used Selection.Tables.Add to add a table
>> into word.
>>
>> How do I move the selection to the end of the table (i.e. next line?)
>> If I were to do Selection.TypeText("blah") the text is inserted in
>> the first cell of the table, which is NOT what I want.
>>
>> I've tried methods like Selection.Move and MoveEnd... but it doesn't
>> seem to work. Maybe I'm using it wrong? Any help would be greatly
>> appreciated,
>>
>> Thank you,
>> Ray
>
> Hi Ray,
>
> Try this:
>
> Dim oTbl As Table
> Set oTbl = ActiveDocument.Tables.Add( _
> Range:=Selection.Range, _
> NumRows:=2, NumColumns:=2)
> oTbl.Select
> With Selection
> .Collapse wdCollapseEnd
> ' Selection is now after the end of oTbl
> .TypeText "blah"
> End With
>
> Personally, I prefer to use the Selection object only for communicating
> with
> the user, and use Range objects for all other work. Here's the equivalent
> with a Range object:
>
> Dim oRg As Range
> Dim oTbl As Table
> Set oTbl = ActiveDocument.Tables.Add( _
> Range:=Selection.Range, _
> NumRows:=2, NumColumns:=2)
> Set oRg = oTbl.Range
> With oRg
> .Collapse wdCollapseEnd
> ' oRg is now after the end of oTbl
> .Text = "blah"
> End With
>
> With this, the text will be after the table, but the cursor will still be
> in
> the first cell of the table. If you want to move it, you can put in
> oRg.Select as the last statement of the macro (with or without collapsing
> it).
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
>
>



Re: Selection.Tables.Add -- moving the selection by Jay

Jay
Tue Aug 23 19:38:06 CDT 2005

Hi Ray,

If you wanted to move the table manually, you'd go to the Table >
Properties dialog and set the "Left indent" value. The code equivalent
of that for a 1" indent is the statement

oTbl.Rows.LeftIndent = InchesToPoints(1)

I'd like to point you to a good reference, but I haven't seen one.
There are several I'd consider fair, including "Word 2000 Developer's
Handbook" by Guy Hart-Davis and "VBA Developer's Handbook" by Ken
Getz.

For C#, the only good article I'm aware of is
http://msdn.microsoft.com/library/en-us/odc_vsto2003_ta/html/OffCSharp.asp.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

On Mon, 22 Aug 2005 21:53:47 -0700, "Ray" <me_raychan.REMOVE@shaw.ca>
wrote:

>Hello Jay,
>Thank you for the info -- it worked great.
>
>Another related question -- when I add the new table, how do I make it keep
>the current selection/paragraph indentation? Even if it doesn't keep it, can
>you tell me how to properly indent the table? Or at least move the left
>anchor position of the first column?
>
>Does anyone have a good reference to Office Automation? My google searches
>hasn't been that kind to me lately. Also, I'm coding this in c#, so samples
>in that language would be very useful too.
>
>Thanks again,
>Ray
>
>"Jay Freedman" <jay.freedman@verizon.net> wrote in message
>news:O4LbYp0pFHA.1028@TK2MSFTNGP09.phx.gbl...
>> Ray wrote:
>>> I'm using office automation in Word.
>>> I have a selection and have used Selection.Tables.Add to add a table
>>> into word.
>>>
>>> How do I move the selection to the end of the table (i.e. next line?)
>>> If I were to do Selection.TypeText("blah") the text is inserted in
>>> the first cell of the table, which is NOT what I want.
>>>
>>> I've tried methods like Selection.Move and MoveEnd... but it doesn't
>>> seem to work. Maybe I'm using it wrong? Any help would be greatly
>>> appreciated,
>>>
>>> Thank you,
>>> Ray
>>
>> Hi Ray,
>>
>> Try this:
>>
>> Dim oTbl As Table
>> Set oTbl = ActiveDocument.Tables.Add( _
>> Range:=Selection.Range, _
>> NumRows:=2, NumColumns:=2)
>> oTbl.Select
>> With Selection
>> .Collapse wdCollapseEnd
>> ' Selection is now after the end of oTbl
>> .TypeText "blah"
>> End With
>>
>> Personally, I prefer to use the Selection object only for communicating
>> with
>> the user, and use Range objects for all other work. Here's the equivalent
>> with a Range object:
>>
>> Dim oRg As Range
>> Dim oTbl As Table
>> Set oTbl = ActiveDocument.Tables.Add( _
>> Range:=Selection.Range, _
>> NumRows:=2, NumColumns:=2)
>> Set oRg = oTbl.Range
>> With oRg
>> .Collapse wdCollapseEnd
>> ' oRg is now after the end of oTbl
>> .Text = "blah"
>> End With
>>
>> With this, the text will be after the table, but the cursor will still be
>> in
>> the first cell of the table. If you want to move it, you can put in
>> oRg.Select as the last statement of the macro (with or without collapsing
>> it).
>>
>> --
>> Regards,
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
>>
>>
>