• Working with a range of table cells (2002 SP3)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Working with a range of table cells (2002 SP3)

    Author
    Topic
    #410815

    In Excel I can refer to a range of cells and apply changes to them without having to select them first.

    I want to Hide all the text in a particular column apart from the header row.

    In Word I know I can use the SetRange method to select that range of cells and then apply the change and I can also cycle through the cell objects and thus apply the change without selecting them.

    But can I combine the 2 and just refer to a range of cells – something like ActiveDocument.tables(1).range(cells(3,2), cells(3,activedocument.tables(1).columns(3).rows.count)).

    Viewing 0 reply threads
    Author
    Replies
    • #886201

      You can use SetRange to set the start and end of a range, for example:

      Dim rng As Range
      Set rng = ActiveDocument.Range
      With ActiveDocument.Tables(1)
      rng.SetRange .Cell(2, 2).Range.Start, .Cell(4, 4).Range.End
      End With

      • #886233

        That’s great, one thing that just caught me out is that I had expected

        rng.SetRange .Cell(2, 2).Range.Start, .Cell(4, 4).Range.End

        to be a square range of 9 cells but in fact, in a 5×5 table for example, it includes 14 cells:

        Row 2 cells 2 to 5
        Row 3 cells 1 to 5
        Row 4 cells 1 to 4

        • #886237

          Sorry, I hadn’t checked thoroughly. As a test to see what the code did, I added a line (which I didn’t post)

          rng.Select

          When you run the code with this line, a square block of 3 by 3 cells within the table is selected. But the range is as you described. Thinking of it, I don’t think there is a workaround. We’re up against a fundamental difference between the Range and Selection objects here. A Range object in Word consists of everything between the start position and the end position. The Selection can have different shapes, and need not be contiguous.

          • #886295

            Yeah, I just tried

            Set rng = ActiveDocument.Tables(1).Range

            With rng.Columns(3)
            rng.SetRange .Cells(2).Range.Start, .Cells(.Cells.Count).Range.End
            rng.Font.Hidden = True
            End With

            and the effect wasn’t restricted to “Columns(3)”

            So I guess I’m gonna have to select the range after all.

            Thanks for your help, couldn’t have done it without you!

      • #886234

        That’s great, one thing that just caught me out is that I had expected

        rng.SetRange .Cell(2, 2).Range.Start, .Cell(4, 4).Range.End

        to be a square range of 9 cells but in fact, in a 5×5 table for example, it includes 14 cells:

        Row 2 cells 2 to 5
        Row 3 cells 1 to 5
        Row 4 cells 1 to 4

    Viewing 0 reply threads
    Reply To: Working with a range of table cells (2002 SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: