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

Posted on 2009-02-15
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 76

    Accepted Solution

    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

    Thank you GrahamSkan!
    Fabulously clear and easy code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now