# Visual Basic > Visual Basic FAQs >  [FAQ's: OD] How do I add/modify/read a table programmatically?

## RobDog888

To programmatically add, modify, or read a table cell in a Word document:




*Word 2003 And VB 6 Code Example (Add Table):*

VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
Public Function AddTable(ByVal iRows As Integer, ByVal iCols As Integer) As Boolean
    
    Dim oApp As Word.Application
    Dim oDoc As Word.Document
    Dim oTable As Word.Table
    Dim oRowHeader As Word.Row
    Dim x As Integer
    
    Set oApp = New Word.Application
    'Open either a blank new document or...
    Set oDoc = oApp.Documents.Add
    'open an exisiting document
    'Set oDoc = oApp.Documents.Open("C:\Document1.doc")
    oDoc.Activate
    'Move to the end of the document
    oApp.Selection.Move Unit:=wdStory
    oApp.Selection.TypeParagraph
    oApp.Selection.TypeParagraph
    'Add a new table
    Set oTable = oDoc.Tables.Add(Range:=Selection.Range, NumRows:=iRows, NumColumns:=iCols, _
    DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow)
    'Format the table and populate
    With oTable
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
        'Populate and format the table header
        Set oRowHeader = oTable.Rows.Item(1)
        oRowHeader.Shading.Texture = wdTextureNone
        oRowHeader.Shading.ForegroundPatternColor = wdColorAutomatic
        oRowHeader.Shading.BackgroundPatternColor = wdColorGray25
        For x = 1 To iCols
            'Bold on for the cell
            .Cell(1, x).Range.Font.Bold = True
            .Cell(1, x).Range.Text = "ColumnHeader" & x
        Next
    End With
    'Clean up and leave document open
    Set oRowHeader = Nothing
    Set oTable = Nothing
    Set oDoc = Nothing
    Set oApp = Nothing
    
End Function

----------


## RobDog888

*Word 2003 And VB 6 Code Example (Read Table):*

VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
'Add a MS Flexgrid control and a Command Button.
Private Sub Command4_Click()
     Dim oApp As Word.Application
    Dim oDoc As Word.Document
    Dim oTable As Word.Table
    Dim x As Integer
    Dim y As Integer
    
    Set oApp = New Word.Application
    'open an exisiting document
    Set oDoc = oApp.Documents.Open("C:\Document1.doc")
    Set oTable = oDoc.Tables.Item(1)
    With oTable
        MSFlexGrid1.AllowUserResizing = flexResizeColumns
        MSFlexGrid1.Cols = .Columns.Count
        MSFlexGrid1.Rows = .Rows.Count
        MSFlexGrid1.Clear
        'Set up header row
        For y = 0 To .Columns.Count - 1
            MSFlexGrid1.Row = 0
            MSFlexGrid1.Col = y
            'Trim off the carriage return (chr 13 and 7)
            MSFlexGrid1.Text = Replace(.Cell(1, y + 1).Range.Text, Chr(13) & Chr(7), vbNullString)
        Next
        'Read the cells data into the ms flexgrid control
        For x = 1 To .Rows.Count - 1
            For y = 0 To .Columns.Count - 1
                MSFlexGrid1.Col = y
                MSFlexGrid1.Row = x
                MSFlexGrid1.Text = Replace(.Cell(x + 1, y + 1).Range.Text, Chr(13) & Chr(7), vbNullString)
            Next
        Next
    End With
    'Clean up and close document
    Set oTable = Nothing
    oDoc.Close SAveChanges:=False
    Set oDoc = Nothing
    oApp.Quit
    Set oApp = Nothing
 End Sub

----------


## RobDog888

*Word 2003 And VB 6 Code Example (Read Table):*

VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
'Add one RichTextBox control (RichTextBox1) to your form
Private Sub Command1_Click()
    
    Dim oApp As Word.Application
    Dim oDoc As Word.Document
    Dim oTable As Word.Table
    
    Set oApp = New Word.Application
    Set oDoc = oApp.Documents.Open("C:\Development\Tips\Word FAQ\Word FAQ.doc")
    Set oTable = oDoc.Tables.Item(1)
    oTable.Select
    oApp.Selection.Copy
    RichTextBox1.TextRTF = Clipboard.GetText(vbCFRTF)
    'Clean up and close doocument
    Set oTable = Nothing
    oDoc.Close SaveChanges:=False
    Set oDoc = Nothing
    oApp.Quit
    Set oApp = Nothing
    
End Sub

----------


## RobDog888

*Word 2003 And VB 6 Code Example (Write Table):*

VB Code:
Option Explicit
'Add a reference to MS Word xx.0 Object Library
Public Sub Write2Table()
     Dim oApp As Word.Application
    Dim oDoc As Word.Document
    Dim oTable As Word.Table
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer
    
    Set oApp = New Word.Application
    'Open an exisiting document
    Set oDoc = oApp.Documents.Open("C:\Document1.doc")
    Set oTable = oDoc.Tables.Item(2) 'Second table for ex.
    With oTable
        z = 1
        'Populate the data cells
        For x = 2 To oTable.Rows.Count
            For y = 1 To oTable.Columns.Count
                'Add cell color formatting
                .Cell(x, y).Range.Font.ColorIndex = z
                'Write to the table cell
                .Cell(x, y).Range.Text = "Meow " & x - 1
                z = z + 1
            Next
        Next
    End With
    'Clean up
    Set oTable = Nothing
    oDoc.Save
    'Leave the document open for reviewing
    Set oDoc = Nothing
    Set oApp = Nothing
 End Sub

----------

