Selection and Adding New Tables to a New Document

S

StevenM

I’m writing a macro where I need to create a series of tables in a new MS
Word (2000) document and import text into each table. Each table needs a
number of blank lines between them. Each table has one column and three rows.
The following code worked fine for the first table.

Set oTbl = objNewDoc.Tables.Add(Range:=Selection.Range, numrows:=3,
numcolumns:=1)
oTbl.Range.Borders.Enable = False
oTbl.Cell(1, 1).Range.Text = "Add text here."
With oTbl.Cell(2, 1).Range
..Style = "Diagram"
..Text = “Add a chess diagram font text here.â€
End With
oTbl.Cell(3, 1).Range.Text = “Add text here.â€

But I’ve ran into two problems. How do I add blank lines (a series of empty
paragraphs) after this table? And how do I add another table following those
blank lines? I tried looping through this code a second time only to get the
second table inside the first table. I figure I’m not understanding how
“Selection†works. Any help here would be greatly appreciated.

Thanks!
Steven Craig Miller
 
J

Jean-Guy Marcil

StevenM said:
I’m writing a macro where I need to create a series of tables in a new MS
Word (2000) document and import text into each table. Each table needs a
number of blank lines between them. Each table has one column and three rows.
The following code worked fine for the first table.

Set oTbl = objNewDoc.Tables.Add(Range:=Selection.Range, numrows:=3,
numcolumns:=1)
oTbl.Range.Borders.Enable = False
oTbl.Cell(1, 1).Range.Text = "Add text here."
With oTbl.Cell(2, 1).Range
.Style = "Diagram"
.Text = “Add a chess diagram font text here.â€
End With
oTbl.Cell(3, 1).Range.Text = “Add text here.â€

But I’ve ran into two problems. How do I add blank lines (a series of empty
paragraphs) after this table? And how do I add another table following those
blank lines? I tried looping through this code a second time only to get the
second table inside the first table. I figure I’m not understanding how
“Selection†works. Any help here would be greatly appreciated.

This adds three tables at the current insertion point, each followed by four
empty paragraphs (¶):

Sub Test()

Dim oTbl As Table
Dim i As Long
Dim rgeTble As Range
Dim objNewDoc As Document

Set objNewDoc = ActiveDocument
Set rgeTble = Selection.Range

i = 1

For i = 1 To 3
Set oTbl = objNewDoc.Tables.Add(Range:=rgeTble, numrows:=3, numcolumns:=1)
With oTbl
.Range.Borders.Enable = False
.Cell(1, 1).Range.Text = "Add text here."
With .Cell(2, 1).Range
.Style = "Diagram"
.Text = "Add a chess diagram font text here."
End With
.Cell(3, 1).Range.Text = "Add text here."
Set rgeTble = .Range
With rgeTble
.MoveEnd wdCharacter, 1
.InsertAfter Chr(13) & Chr(13) & Chr(13) & Chr(13)
.Collapse wdCollapseEnd
End With
End With
Next

End Sub


You may want to add some checking in the code to make sure that the current
insertion point is not inside a table or is a selected object...
 
S

StevenM

To: Jean-Guy Marcil,

Thank you, that is just what I needed! Thank you very much.

Steven Craig Miller
 

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