Creating Tables
Tables.Add
Dim oTable As Word.Table
Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=3, _
NumColumns:=4, _
DefaultTableBehavior:=wdDefaultTableBehavior.wdWord8TableBehavior, _
AutoFitBehavior:=wdAutoFitBehavior.wdAutoFitWindow)
Range - The range where you want the table to appear. The table replaces the range, if the range isn't collapsed.
NumRows - The number of rows you want to include in the table.
NumColumns - The number of columns you want to include in the table.
DefaultTableBehavior - Sets a value that specifies whether Microsoft Word automatically resizes cells in tables to fit the cells' contents (AutoFit).
AutoFitBehavior - Sets the AutoFit rules for how Word sizes tables.
ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4
Sub NewTable()
Dim docNew As Word.Document
Dim tblNew As Word.Table
Dim intX As Integer
Dim intY As Integer
Set docNew = Documents.Add
Set tblNew = docNew.Tables.Add(Selection.Range, 3, 5)
With tblNew
For intX = 1 To 3
For intY = 1 To 5
.Cell(intX, intY).Range.InsertAfter "Cell: R" & intX & ", C" & intY
Next intY
Next intX
.Columns.AutoFit
End With
End Sub
Nested Tables
Public Sub InsertNested()
Documents.Add
ActiveDocument.Tables.Add Selection.Range, 3, 3, wdWord9TableBehavior, wdAutoFitContent
With ActiveDocument.Tables(1).Range
.Copy
.Cells(1).Range.Text = .Cells(1).NestingLevel
.Cells(5).Range.PasteAsNestedTable
With .Cells(5).Tables(1).Range
.Cells(1).Range.Text = .Cells(1).NestingLevel
.Cells(5).Range.PasteAsNestedTable
With .Cells(5).Tables(1).Range
.Cells(1).Range.Text = .Cells(1).NestingLevel
End With
.Columns(2).Select
Selection.TopLevelTables(1).Select
End With
End With
End Sub
oTable.PreferredWidthType = wdPreferredWidthType.wdPreferredWidthPoints
oTable.PreferredWidth = 50
oTable.Rows.VerticalPosition = 0 (single value)
oTable.Rows.Alignment = wdRowAlignment.wdAlignRowLeft
oTable.Rows.HorizontalPosition = 0 (single value)
oTable.Rows.HeightRule = wdRowHeightRule.wdRowHeightAuto
oTable.Rows.RelativeHorizontalPosition = wdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage
oTable.Rows.RelativeVerticalPosition = wdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
What is the difference between ??
oTable.Style = objstyle
oTable.Range.Style = objstyle
Sub Testing()
Dim oTable As Word.Table
Set oTable = Application.ActiveDocument.Tables.Add(Range:=Application.Selection.Range, NumRows:=1, NumColumns:=1)
Debug.Print objTable.PreferredWidthType
End Sub
Public Sub TestingDis_Insert()
Dim oRange As Word.Range
Dim iNoOfSections As Integer
Dim objTable As Word.Table
Dim sngTableWidthPoints As Single
Application.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = 0
Set oRange = Application.ActiveDocument.Range
oRange.Collapse (Word.WdCollapseDirection.wdCollapseEnd)
oRange.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
'get the total number of sections now in the document
iNoOfSections = Application.ActiveDocument.Sections.Count
'unlink header and footer from the previous section
With Application.ActiveDocument.Sections(iNoOfSections).Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Delete
End With
With Application.ActiveDocument.Sections(iNoOfSections).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Delete
End With
'adjust the bottom margin of this section
With Application.ActiveDocument.Sections(iNoOfSections)
.PageSetup.BottomMargin = Application.CentimetersToPoints(1.52)
End With
'get a range that represents the end of the document
Set objRange = Application.ActiveDocument.Range
objRange.Collapse (WdCollapseDirection.wdCollapseEnd)
' add a table with 1 column and 3 rows
Set oTable = Application.ActiveDocument.Tables.Add(Range:=oRange, NumRows:=3, NumColumns:=1)
sngTableWidthPoints = Application.ActiveDocument.PageSetup.PageWidth - _
(Application.ActiveDocument.PageSetup.LeftMargin + _
Application.ActiveDocument.PageSetup.RightMargin) + 8
With oTable
.PreferredWidthType = wdPreferredWidthPoints
.PreferredWidth = sngTableWidthPoints
' With .Rows
' .VerticalPosition = Word.WdTablePosition.wdTableBottom
' .RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
' End With
With .Range.Font
.Name = "Expert Sans Regular"
.Size = 8
End With
End With
'get a range that represents the end of the document
Set oRange = Application.ActiveDocument.Range
oRange.Collapse (Word.WdCollapseDirection.wdCollapseEnd)
'selecting the line
oRange.Expand (WdUnits.wdParagraph)
oRange.Font.Name = "Expert Sans Regular"
oRange.Font.Size = 8
oRange.InsertParagraphAfter
oRange.Select
'get a range that represents the end of the document
Set oRange = Application.ActiveDocument.Range
oRange.Collapse (Word.WdCollapseDirection.wdCollapseEnd)
'add a table with 1 column and 1 row
Set oTable = Application.ActiveDocument.Tables.Add(Range:=objRange, NumRows:=1, NumColumns:=1)
With oTable
.PreferredWidthType = wdPreferredWidthPoints
.PreferredWidth = sngTableWidthPoints 'use the same width we calculated above
With .Range.Font
.Name = "Expert Sans Regular"
.Size = 8
End With
End With
End Sub
© 2025 Better Solutions Limited. All Rights Reserved. © 2025 Better Solutions Limited TopPrevNext