Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2009-02-15
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:
Question by:PioneerSolutions
LVL 77

Accepted Solution

GrahamSkan earned 2000 total points
ID: 23644429
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


Author Closing Comment

ID: 31547071
Thank you GrahamSkan!
Fabulously clear and easy code.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question