Word 2007: Using a relative reference to access adjacent cells in a table

Posted on 2009-02-17
Last Modified: 2013-11-25
I am working with a table in Microsoft Word (2007)
1) Using VBA how can I access cells adjacent to the active cell using relative referencing
2) To add to the question, suppose the active cell to be in a nested table, how would I navigate the table object model to access cells in the parent table, where ever they might be, without resorting to direct referencing (e.g. ActiveDocument.Tables.Cell(2,3) &.etc)

Please see attached example or just follow this link to see an illustration of the question

Please provide a snippet for each cell in the illustration. Thank you!


Question by:PioneerSolutions
    LVL 76

    Accepted Solution

    This macro shows a way of working with the outer table.
    Sub NestedTables()
        Dim wrdCellInner As Word.Cell
        Dim wrdCellOuter As Word.Cell
        Dim wrdOuterTable As Word.Table
        Dim wrdRange As Word.Range
        Set wrdRange = Selection.Range
        If wrdRange.Cells.Count Then
            Set wrdCellInner = wrdRange.Cells(1)
            If wrdCellInner.NestingLevel = 2 Then
                wrdRange.Expand wdTable
                wrdRange.Collapse wdCollapseEnd
                Set wrdCellOuter = wrdRange.Cells(1)
                Set wrdOuterTable = wrdRange.Tables(1)
                wrdOuterTable.Rows(wrdCellOuter.RowIndex).Height = CentimetersToPoints(3)
                wrdOuterTable.Rows(wrdCellOuter.RowIndex + 3).Height = CentimetersToPoints(3)
            End If
        End If
    End Sub

    Open in new window


    Author Closing Comment

    Thank you GrahamSkan!

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    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…
    This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
    This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now