vbams-wordword-2003

How to modify a table in word with VBA


I have tables that I am creating, and I want to be able to modify them through the code in the VBA.

What I need to do to the tables is merge and resize some cells and also add text to some of the cells.


Solution

  • To append on what Lance was saying, here's an example of Merging Cells and setting text in the value of those merged cells:

    Dim myCells As Range
    With ActiveDocument
        Set myCells = .Range(Start:=.Tables(1).Cell(1, 1).Range.Start, End:=.Tables(1).Cell(1, 3).Range.End)
        myCells.Select
    End With
    
    Selection.Cells.Merge
    
    
    ActiveDocument.Tables(1).Cell(Row:=1, Column:=1).Range.Text = "Value for Merged Cells"
    

    NOTE: The table in this example had three columns and two rows