How to change a table in a word using VBA

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

What I need to do with the tables is merging and resizing some cells, as well as adding text to some cells.

+3
source share
2 answers

To add what Lance said, here is an example of Merging cells and setting the text to the value of these 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

+5
source

You need to access the Table object, for example

ActiveDocument.Tables(1).Cell(Row:=2, Column:=2).Range.Text

or

<some Word.Document here>.
  Content.Tables(1).Columns.SetWidth <columnwidthhere>, wdAdjustSameWidth    
+3
source

All Articles