none
Table has vertically merged cells exception. RRS feed

  • Question

  • hi

    i am working on a word addin project using C#(VSTO).

    the problem is stated below.

    i have a two row table consisting 4 coloumns when i merge row for first 2 cells of my table and then access the last row in the table using code below.

    Range.Row.Last

    it gives the following exception

    Cannot access individual rows in this collection because the table has vertically merged cells.

    if this is a limitation in word object model then suggest me any workaround.


    • Edited by kashii Saturday, August 17, 2013 9:30 AM merging of cell to merging of row
    Saturday, August 17, 2013 8:07 AM

Answers

  • Hi,

    We can access cells from the table which has vertically merged cells.

    As Rich provided VBA sample, here is C# for this issue:

     Table table=Globals.ThisDocument.Tables[1];
    
                Range range = table.Range;
    
                for (int i = 1; i <= range.Cells.Count; i++)
    
                {
    
                    if(range.Cells[i].RowIndex == table.Rows.Count)
    
                        range.Cells[i].Range.Text = range.Cells[i].RowIndex + ":" + range.Cells[i].ColumnIndex;
    
                }
    

     

    Tuesday, August 20, 2013 7:54 AM

All replies

  • You can use the error code to determine how to deal with the situation. Here is an example:

    Sub findRowsInMergedCells()
        Dim doc As Word.Document, rng As Word.Range
        Set doc = ActiveDocument
        Set rng = doc.Content
        On Error Resume Next
        
        Set rng = rng.Tables(1).Range
        Set rng = rng.Tables(1).Rows.Last
        If Err.Number = 5991 Then
            Set rng = rng.Tables(1).Columns(1).Cells(1).Range
            Debug.Print rng.Text
        End If
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Saturday, August 17, 2013 1:50 PM
  • Hi,

    We can access cells from the table which has vertically merged cells.

    As Rich provided VBA sample, here is C# for this issue:

     Table table=Globals.ThisDocument.Tables[1];
    
                Range range = table.Range;
    
                for (int i = 1; i <= range.Cells.Count; i++)
    
                {
    
                    if(range.Cells[i].RowIndex == table.Rows.Count)
    
                        range.Cells[i].Range.Text = range.Cells[i].RowIndex + ":" + range.Cells[i].ColumnIndex;
    
                }
    

     

    Tuesday, August 20, 2013 7:54 AM

  • Split table below merged cells and process unmerged part. Than join the merged table part with processed table part. It's fast and simple. 

    Example on https://wordpress.com/view/bhbpbg.wordpress.com

    bhbp.bg team



    • Edited by bhbp.bg Thursday, December 14, 2017 9:12 PM
    Thursday, December 14, 2017 9:10 PM