We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Word 2007: Using vba to access a table nested in another table

Medium Priority
Last Modified: 2013-11-25
I am working with word table. Some of the cells contain nested tables, which I want to access and manipulate using vba.
For the example attached, can you please provide a code snippet to loop through the table and test if a cell contains a nested table. If it does, to loop through the nested table and determine which cells contain the text "Hello World".
(I also posted an illustration of the question:
Watch Question

Top Expert 2012
Try this. It has been tested in Word 2003, but it should work in 2007.
Sub ColourCells()
    Dim tbl As Table
    Dim cl As Cell
    Dim cl1 As Cell
    For Each cl In ActiveDocument.Tables(1).Range.Cells
        If cl.Tables.Count = 1 Then
            cl.Shading.BackgroundPatternColor = wdColorGray50
            Set tbl = cl.Tables(1)
            For Each cl1 In tbl.Range.Cells
                If Left$(cl1.Range.Text, 11) = "Hello World" Then
                    cl1.Shading.BackgroundPatternColor = wdColorYellow
                End If
            Next cl1
        End If
    Next cl
End Sub

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thank you GrahamSkan!
Fabulously clear and easy code.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.