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
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