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)

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

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


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


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext