Place cursor after newly created Table

P

Piet

Hi there.
I have written a macro that generates a table in word with information
that are obtained from a self made dialog. As the last step in the
macro, I enumerate the tables automatically with the header above the
table, so the cursor sits right before the table when the macro is
ended (the userform is still visible). I would like to have the
possibility to create several tables while the dialog box is still
open. For that purpose I have to place the cursor in the first line
after the table to that the second generated table will not interfere
with the first one. Is there a VBA command to achieve this? I think it
will be something like selection.goto what:= table which:= atEnd or
something but I have no idea hjow this works exactly.
Can anybody help?
Many thanks in advance
Piet
 
P

Peter Hewett

Hi Piet

It's reasonably straightforward, the following code will insert a table after the current
table. The code expects the selection object to be in or include the table you want the
new table inserted after:

Public Sub InsertTableAfterTable()
Dim rngTable As Word.Range

' Move to the end of the current table
Set rngTable = Selection.Tables(1).Range
rngTable.Collapse wdCollapseEnd
rngTable.Move wdCharacter, 1

' Add new table - 5 rows, 3 columns
rngTable.Tables.Add rngTable, 5, 3
End Sub

HTH + Cheers - Peter


(e-mail address removed) (Piet), said:
 
P

Piet

Peter Hewett said:
Hi Piet

It's reasonably straightforward, the following code will insert a table after the current
table. The code expects the selection object to be in or include the table you want the
new table inserted after:

Public Sub InsertTableAfterTable()
Dim rngTable As Word.Range

' Move to the end of the current table
Set rngTable = Selection.Tables(1).Range
rngTable.Collapse wdCollapseEnd
rngTable.Move wdCharacter, 1

' Add new table - 5 rows, 3 columns
rngTable.Tables.Add rngTable, 5, 3
End Sub

Hi Peter,
many thanks for the quick response!
The collapse-method and the wdCollapseEnd constant were the missing
(of which I have never heard before).
Since the current table (after which I want to place the cursor) is
assigned to an object, the solution is now really simple:
myTableObj.Select
Selection.Collapse wdCollapseEnd
Selection.InsertAfter Chr(13)
Selection.Move wdCharacter,1
That does exactly what I want! Many thanks!
Best wishes

Piet
 

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