Microsoft KB Archive/251015

From BetaArchive Wiki
Knowledge Base

WD2000: Macro Example for Formatting Table Cells That Contain Text

Article ID: 251015

Article Last Modified on 10/11/2006


  • Microsoft Word 2000 Standard Edition

This article was previously published under Q251015


The following macro is an example of how to select each cell in a table, test the cell for text, and then format only the cells that contain text.


Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

Sub EditMyTable()

   ' Test if in a table.
   If Selection.Information(wdWithInTable) Then

   ' Set myRange as the document work area.
   Set myRange = Selection.Range

   ' Set oTable as the selected table.
   Set oTable = myRange.Tables(1)

   ' Loop through the cells of the table.
   For Each acell In oTable.Range.Cells

   ' Sets myCell equal to the text minus the end-of-cell marker.
   Set myCell = ActiveDocument.Range(Start:=acell.Range.Start, _
      End:=acell.Range.End - 1)

      ' Formats cell if # of characters greater than zero.
      If Len(Trim(myCell)) > 0 Then
         acell.Shading.Texture = wdTexture10Percent
      End If

      ' Goes to next cell of table
      Next acell


      ' Returns a message if insertion point not in a table.
      MsgBox "You Are Not In A Table"

      GoTo Bottom:
   End If

   MsgBox "This Macro Is Finished"


End Sub

For additional information about programming assistance, click the article numbers below to view the articles in the Microsoft Knowledge Base:

212623 WD2000: Macro Programming Resources

226118 OFF2000: Programming Resources for Visual Basic for Applications

Additional query words: vba

Keywords: kbhowto kbmacroexample kbprogramming KB251015