Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Selection and Adding New Tables to a New Document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
StevenM - 25 Mar 2008 20:24 GMT
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
Jean-Guy Marcil - 25 Mar 2008 20:54 GMT
> 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
[quoted text clipped - 16 lines]
> 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...
StevenM - 25 Mar 2008 21:54 GMT
To: Jean-Guy Marcil,

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

Steven Craig Miller
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.