Advanced Techniques


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.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrev