[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

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

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!


1 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

PioneerSolutionsAuthor Commented:
Thank you GrahamSkan!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now