Microsoft Word Table Object VBA Code example

Microsoft Word Table Object VBA Code example

Table

Microsoft Word VBA offers very powerful object called Table and a collection Tables as part of a Document Object. Table is very useful to populate account, time table, categorization of objects etc. Tables is a collection which can have many nested tables and can be iterated using for each loop. It allows developer to manipulate all the properties during development of a table in a document like formatting, rows, columns, borders, cells, range, size, width, height etc. Latest office release provides more robust and enrich properties to the table object.

Code example

Public Sub CreateTable()
    Dim oDoc As Document
    Set oDoc = ActiveDocument
    Dim oTable As Table
    Set oTable = oDoc.Tables.Add(Range:=oDoc.Range(Start:=0, End:=0), NumRows:=3, NumColumns:=3)
    Set oTable = Nothing
End Sub

This code will create a table of 3×3 but table will not have borders and will not be visible on the document but you can see at the starting of document the table is inserted. To prove I have given it border so you can see the real output:

Let’s insert some values in table by extending existing code:

Code example

Public Sub CreateAndInsertValuesInTable()
    Dim oDoc As Document
    Set oDoc = ActiveDocument
    Dim oTable As Table
    Dim oCel As Cell
    Dim oCounter As Integer
    oCounter = 1
    Set oTable = oDoc.Tables.Add(Range:=oDoc.Range(Start:=0, End:=0), NumRows:=3, NumColumns:=3)
    'Adding value in each cell
    For Each oCel In oTable.Range.Cells
        oCel.Range.InsertAfter "Cell number: " & oCounter
        oCounter = oCounter + 1
    Next oCel
    Set oTable = Nothing
End Sub

Output

Now let’s format the table in real beautiful colors and patterns:

Code example

Public Sub CreateTableAndFormat()
    Dim oDoc As Document
    Set oDoc = ActiveDocument
    Dim oTable As Table
    Dim oCel As Cell
    Dim oCounter As Integer
    oCounter = 1
    Set oTable = oDoc.Tables.Add(Range:=oDoc.Range(Start:=0, End:=0), NumRows:=3, NumColumns:=3)
    'Adding value in each cell
    For Each oCel In oTable.Range.Cells
        oCel.Range.InsertAfter "Cell number: " & oCounter
        oCounter = oCounter + 1
    Next oCel
    'Auto format table
    oTable.AutoFormat Format:=wdTableFormatColorful3, ApplyBorders:=True, ApplyFont:=True, ApplyColor:=True
    Set oCel = Nothing
    Set oTable = Nothing
End Sub

Output

Iterate tables

Public Sub IteratingAllTablesInActiveDoc()
    Dim oDoc As Document
    Set oDoc = ActiveDocument
    Dim oTable As Table
    'Avialbe tables in active document
    Debug.Print "Total tables in Active Document : " & oDoc.Tables.Count
    For Each oTable In oDoc.Tables
        'Do whatever you want here
    Next oTable
	Set oTable=Nothing
End Sub

Text to table

Sub ConvertExistingTextToTable()
	With Documents.Add.Content
		.InsertBefore "one" & vbTab & "two" & vbTab & "three" & vbCr
		.ConvertToTable Separator:=Chr(9), NumRows:=1, NumColumns:=3
	End With
End Sub

Nested table

Another challenge of reading a table which is created or available inside a table. Microsoft Word supports nested table concept but this scenario a bit different and challenging to the developer where he may need to write code to trap a table available inside Cell.

Code example

Public Sub ReadNestedTableWithinCell()
    Dim parentTable As Table
    Dim childTable As Table
    Dim oCell As Cell
    'Set Parent Table object
    Set parentTable = ActiveDocument.Tables(1)
    'Set Cell Reference of parent table of row 2 and cell 1 where nested table is
    Set oCell = parentTable.Rows(2).Cells(1)
    'Check if cell contains a table
    If oCell.Tables.Count > 0 Then
        'Get Child Table available inside cell
        Set childTable = oCell.Tables(1)
		childTable.Select
    End If
End Sub

Output

Next : Bookmark Object in Word VBA

Leave a Reply

Your email address will not be published. Required fields are marked *