Multiple Tables

L

LEU

I have the following macro that creates a table in my document. My problem is
this macro works fine if there is not another table in my document. How do I
keep this table from being added to the bottom of an existing Table(1)? My
existing document could have one table or even twenty tables in it already.

Private Sub CommandButton1_Click()
On Error GoTo endthis
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=1,
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Selection.Tables(1).Rows.SetLeftIndent LeftIndent:=8, RulerStyle:=wdAdjustNone
Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=432,
RulerStyle:=wdAdjustNone
Selection.Tables(1).Borders.OutsideLineWidth = wdLineWidth150pt
Selection.ParagraphFormat.Style = "Normal"
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Size = 18
Selection.Font.Color = wdColorRed
Selection.Font.Bold = wdToggle
Selection.Font.Underline = wdUnderlineSingle
Selection.TypeText Text:="Warning"
Selection.Font.Underline = wdUnderlineNone
Selection.Font.Bold = wdToggle
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeBackspace
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 4
.SpaceBeforeAuto = False
.SpaceAfter = 6
.SpaceAfterAuto = False
End With
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Cells.Split NumRows:=1, NumColumns:=2, MergeBeforeSplit:=False
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.MoveRight Unit:=wdCell
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 4
.SpaceBeforeAuto = False
.SpaceAfter = 4
.SpaceAfterAuto = False
End With
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="POTENTIAL HAZARDS"
Selection.MoveRight Unit:=wdCell
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 4
.SpaceBeforeAuto = False
.SpaceAfter = 4
.SpaceAfterAuto = False
End With
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="CONTROLS"
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Cells.Split NumRows:=1, NumColumns:=2, MergeBeforeSplit:=False
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
End With
With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = ChrW(61623)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleBullet
.NumberPosition = InchesToPoints(0)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.25)
.TabPosition = InchesToPoints(0.25)
.ResetOnHigher = 0
.StartAt = 1
With .Font
.Name = "Symbol"
End With
.LinkedStyle = ""
End With
ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
wdBulletGallery).ListTemplates(1), ContinuePreviousList:=False,
ApplyTo:= _
wdListApplyToWholeList, DefaultListBehavior:=wdWord9ListBehavior
rownum = ActiveDocument.Tables(1).Rows.Count + 20
i = 0
ii = 3
Do Until ii > rownum
ActiveDocument.Tables(1).Rows.Add
ActiveDocument.Tables(1).Cell(ii, 1).Select
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
End With
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
wdBulletGallery).ListTemplates(1), ContinuePreviousList:=False,
ApplyTo:= _
wdListApplyToWholeList, DefaultListBehavior:=wdWord9ListBehavior
ActiveDocument.Tables(1).Cell(ii, 1).Range = UserForm1.ListBox2.List(i, 0)
ActiveDocument.Tables(1).Cell(ii, 2).Select
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.SpaceBefore = 2
.SpaceBeforeAuto = False
.SpaceAfter = 2
.SpaceAfterAuto = False
End With
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
wdBulletGallery).ListTemplates(1), ContinuePreviousList:=False,
ApplyTo:= _
wdListApplyToWholeList, DefaultListBehavior:=wdWord9ListBehavior
ActiveDocument.Tables(1).Cell(ii, 2).Range = UserForm1.ListBox2.List(i, 1)
ii = ii + 1
i = i + 1
Loop
endthis:
ActiveDocument.Tables(1).Rows.Last.Delete
ActiveDocument.Tables(1).Rows.Last.Delete
Unload UserForm1
Exit Sub
End Sub
 
D

Doug Robbins - Word MVP

You need to move the select to the location where you want to insert the
table and then use

Selection.InsertBefore vbCr
Selection.Collapse wdCollapseEnd
ActiveDocument.Tables.Add Range:=Selection.Range, etc.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

LEU

Doug,

The lines I’m having problems with are these:
ActiveDocument.Tables(1).Cell(ii, 1).Range = UserForm1.ListBox2.List(i, 0)
ActiveDocument.Tables(1).Cell(ii, 2).Select

It loads the info from my userform1 LisBox2.List into Table(1) instead of
the Table I just created in the first part of my macro. How do I tell it to
load ListBox2.List into the new Table and not Table(1)?
 

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

Similar Threads

Please help!!! No replies - Posting query again 0
Bullet Styles Macro 0
TOC Help 0
TOC Problem 0
Table of Content 1
A macro to increase line spacing 1
set hanging indent for second TOC 1
TOC problem 0

Top