Code Snippets


All cell values in a table end in Chr(13) & Chr(7)


Be careful when using the VBA code Selection.MoveEnd as you can extend a selection by two characters and end up selecting two whole rows in a table


When you are at the "endofrowmark" in a table you are still considered within the table (ie selection.information(wdwithintable) = True


Dim objTable As Word.Table 
objTable = ActiveDocument.Range.Tables.Add( , row, column)


If objRange.Information(wdInformation.wdWithinTable) = True Then 

End If


Range Object

Dim objCell As Word.Cell 
objCell = objTable.Cell(row,column)
objCell.Range.Text = "some text"


objTable.Row(1).Range.Bold = 1 ' True  
objTable.Row(1).Range.Bold = 0 ' False
objTable.Row(1).Range.Bold = - 1'undefined = wdConstants.wdUndefined


objTable.AutoFitBehaviour(wdAutoFitBehavior.wdAutoFitContent) 

rows(1).spacebetweencolumns = 0 
documents("Temp.doc").tables(3)
activedocument.tables(1).range.cells.range.insertbefore text:="cell" & 5
.rows.allowbreakacrosspages = false
.converttotable seperator:=wdseperatebytabs,
                        numcolumns:="",
                        numrows:="",
                        format:=wdtableformatnone,
                        applyborders:=true,
                        applyshading:=true,
                        applyfont:=true,
                        applycolor:=true,
                        applyheadingrows:=true,
                       applylastrow:=false,
                       applyfirstcolumn:=true,
                       aplylastcolumn:=false,
                      autofit:=false

Select Continuous Cells

Set objRange = ActiveDocument.Range(Start:=.Tables(1).Cell(2,2).Range.Start, _ 
                                                End:=.Tables(1).Cell(4,4).Range.End)

This will create a range for a continuous group of cells.
The start must occur before the end, both in terms of rows and columns



The Row, Rows and Table objects have a ConvertToText method that converts table data to text and returns a range object representing the text.


expression.ConvertToText(separator) 

separator - optional character to delimit the text


Set objTable = ActiveDocument.Tables(1) 
Set objRange = objTable.ConvertToText(wdTableFieldSeparator.wdSeparateByParagraphs)

Selection.MoveRight Unit:=wdUnits.wdCell 
Selection.MoveRight Unit:=wdUnits.wdCell, _
                    Count:=3
Selection.MoveRight Unit:=wdUnits.wdCell, _
                    Count:=3, _
                    Extend:=wdMovementType.wdExtend

Move to the first table in the document

Selection.GoTo What:=wdGoToTable, _ 
               Which:=wdGoToFirst, _
               Count:=1, _
               Name:=""


How many tables in a document

ActiveDocument.Tables.Count 


Removes borders from active table

Selection.Tables(1).Borders.Enable = False 


Get the Index number of the current table

Define a range from the start of the document to the end of the first selected paragraph

Msgbox (ActiveDocument.Range(0,Selection.Paragraphs(1).Range.End).Tables.Count) 


ActiveWindow.View.TableGridlines = True 

Insert Formula

Selection.InsertFormula Formula:="=SUM(ABOVE)", _ 
                        NumberFormat:="#,##0,00"

There is a built in ConvertToTable command - YES !!!


If (Selection.Information(wdInformation.wdWithInTable) = False) Then 
   Call MsgBox("You are not in a table!")
End If

Returning text from a table cell with no end marker

Set tblTable = ActiveDocument.Tables(1) 
For Each clCell in tblTable.Rows(1).Cells
   Set rgeRange = ActiveDocument.Range(start := clCell.Range.Start, end := clCell.Range.End - 1)
   Call Msgbox(rgeRange.Text)
Next clCell

Adding a formula

Selection.InsertFormula Formula:="=SUM(ABOVE)", NumberFormat:="#,##0.00" 


Turning 'Allow spacing between cells' off with VBA in a Word 2000 table

There seems to be no supported way in VBA to turn the "Allow spacing between cells" Table property off.
You can set the Spacing property to 0 but the resulting table looks very different from how it looks if you de-select the "Allow spacing between cells" checkbox manually using the (Table > Properties) dialog.


Workaround 1

Selection.Tables(1).Spacing = -1 

makes the table look right.
It's a kludge, because if you then look in the dialog you'll see that "Allow spacing" is still ticked - but it's much better than no workaround at all!


Workaround 2

SendKeys "%s{Enter}" 
Dialogs(1080).Show

also works. This second workaround has the advantage that it does de-select the "Allow spacing" checkbox in the dialog - but besides the inherent disadvantage in using SendKeys, the user will see the dialog momentarily appear and disappear.



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