Consulting

Results 1 to 3 of 3

Thread: Border in word table with vertically merged cells

  1. #1
    VBAX Regular
    Joined
    Sep 2018
    Posts
    9
    Location

    Question Border in word table with vertically merged cells

    Hello,

    I am trying to have a macro button on my toolbar that will place a thick gray border on the top of the row my cursor is in. I am running into an error that states:

    "Run-time error '5991'
    Cannot access individual rows in this collection because the table has vertically merged cells."

    This is a very helpful error code, but I don't know how to fix my code to avoid this problem. This error occurs even if my cursor is not in a row that contains merged cells, but somewhere else in the table does.

    This is my code:

    Private Sub darkborderrow()
    Dim oRow As Row
    Set oRow = Selection.Rows(1)
    With oRow.Range.Borders(wdBorderTop)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth300pt
    .Color = wdColorGray60
    End With
    End Sub

    I will also attach a sample document with tables and code that this problem would occur with incase that would be helpful.
    Border Failure Examples.docm

    Thank you for all of your help,
    Emily

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub DarkBorderRow()
    Dim r As Long, c As Long
    With Selection
      r = .Cells(1).RowIndex
      With .Tables(1).Range
        For c = 1 To .Cells.Count
          With .Cells(c)
            If .RowIndex = r Then
              With .Borders(wdBorderTop)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth300pt
                .Color = wdColorGray60
              End With
            ElseIf .RowIndex > r Then
              Exit For
            End If
          End With
        Next
      End With
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Sep 2018
    Posts
    9
    Location
    That worked!

    Thank you Paul!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •