WordBasic Macro: Moving Through Each Cell in a Table

ID: Q122049

The information in this article applies to:

SUMMARY

When you write Word for Windows macros, you may need to move through each table cell applying formatting or inserting text. The following sample macro uses a While loop to move through each cell in the table, formatting the contents of each cell with bold formatting.

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Sub MAIN
If SelInfo(12) = - 1 Then
 TableSelectTable
 StartOfRow
 EditGoTo "\cell"
 FormatFont .Bold = 1
 While NextCell() <> 0
  EditGoTo "\cell"
  FormatFont .Bold = 1
 Wend
End If
End Sub

The following macro instructions are used to move the insertion point to the beginning of the table:

   TableSelectTable
   StartOfRow

The EditGoTo instruction uses the reserved "\cell" bookmark to select the contents of the cell so that formatting can be applied to the text. A While loop is used to move through each cell in the table. The NextCell() function returns a 0 (zero) when the insertion point is within the final cell of the table. The following macro instruction loops through the cells in the table while the NextCell() function returns a value other than 0 (zero):

   While NextCell() <> 0

REFERENCES

"Microsoft Word Developer's Kit," version 6.0, pages 52-53

KBCategory: kbusage kbmacro KBSubcategory: Additional reference words: 6.0 6.0a 6.0c loop table While word6 Wend NextCell cell winword

Version           : 6.0 6.0a 6.0c
Platform          : WINDOWS

Last Reviewed: February 5, 1998