Selection.Tables.Add -- moving the selection

R

Ray

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
 
J

Jay Freedman

Ray said:
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).
 
R

Ray

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
 
J

Jay Freedman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top