none
How Could I Know How Many Cells be merged in one, C#&&Microsoft.Office.Interop.Word? RRS feed

  • Question

  • Context : In my Word Add in project, i want to reDraw MSWord's table.

    Problem: When the merged cells in one row are more than one, I can do nothing to make sure how many cells the one of merged cells contains.

    HELP!

    • Moved by CoolDadTx Thursday, January 22, 2015 5:58 PM Office related
    Thursday, January 22, 2015 2:34 PM

Answers

  • What you can do really depends on the properties of the table and the merged/split cells.

    If, for example, the cell heights and widths have been set to exact values, you can test the cell .Height and .Width properties. If the cell heights and widths have NOT been set to exact values, in addition to testing the cell .RowIndex and .ColumnIndex properties, you can test the cell .Range.Information(wdHorizontalPositionRelativeToPage) and .Range.Information(wdverticalPositionRelativeToPage) properties for those cells plus their neighbours - but you also need to take into account any indenting.

    Word has no property that tells you if a cell has been merged or split - you have to work that out for yourself. I do not know of any generic code libraries for doing this. Do note that, in addition to cells being merged A(x,y) and B(x,y+1), they can also be merged A(x,y) and B(x,y+n), and/or A(x,y) and B(x+n,y), where n is any number.

    If you create a table for testing, with a mix of horizontally-merged and vertically-merged cells, you can use the following VBA macro to get each cell's address - the address is displayed on Word's status bar. As you will see, with horizontally-merged cells, then column index for the next cell on the same row only increases by 1 but the row index for the next cell in the column with vertically-merged cells increases by the number of merged cells.

    Sub CellAddress()
    'This macro displays the address of a table cell on Word's status bar
    If Selection.Information(wdWithInTable) = True Then
      If Selection.Cells(1).ColumnIndex > 26 Then
        StatusBar = "Cell Address: " & Chr(64 + Int(Selection.Cells(1).ColumnIndex / 26)) & _
        Chr(64 + (Selection.Cells(1).ColumnIndex Mod 26)) & Selection.Cells(1).RowIndex
      Else
        StatusBar = "Cell Address: " & Chr(64 + Selection.Cells(1).ColumnIndex) & _
        Selection.Cells(1).RowIndex
      End If
    End If
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by zzGuiJi Friday, January 23, 2015 5:08 AM
    Friday, January 23, 2015 4:09 AM

All replies

  • Since Word table cells can be split or merged, and columns can have varying widths, you can use the .Uniform property to test whether the table has any of those qualities. When it comes to working with merged and split cells, you can use the .RowIndex and .ColumnIndex properties to test whether all of the cell's neighbours have the corresponding .RowIndex and .ColumnIndex values. This can require a fair bit of work to implement. If you are creating the table from scratch, you will find it easier to create one with a uniform layout, then merge the appropriate cells. If you are trying to replicate an existing table, it's generally easier to copy it (or use the FormattedText method), then delete the duplicate's contents (if necessary).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 23, 2015 1:51 AM
  • Thank you for your advice.

    I want to redraw a MS-Word table to another container(maybe convert to a DataGridView or a table in HtmlPage). I work with C# too little, so i didn't know the shortcut to complete this mission. Now, If i didn't know How many cells be merged to one in a multi-merged-cells row, i could not to redraw.

    For exmaple: If one row has two column-merged cells A(x,y) and B(x,y+1), then i could not know which column should B begin to be drawn. Sertainly i did not know the table's structure before.

    If you know any way to solve this problem, help me please! T.T


    • Edited by zzGuiJi Friday, January 23, 2015 3:42 AM
    Friday, January 23, 2015 3:35 AM
  • What you can do really depends on the properties of the table and the merged/split cells.

    If, for example, the cell heights and widths have been set to exact values, you can test the cell .Height and .Width properties. If the cell heights and widths have NOT been set to exact values, in addition to testing the cell .RowIndex and .ColumnIndex properties, you can test the cell .Range.Information(wdHorizontalPositionRelativeToPage) and .Range.Information(wdverticalPositionRelativeToPage) properties for those cells plus their neighbours - but you also need to take into account any indenting.

    Word has no property that tells you if a cell has been merged or split - you have to work that out for yourself. I do not know of any generic code libraries for doing this. Do note that, in addition to cells being merged A(x,y) and B(x,y+1), they can also be merged A(x,y) and B(x,y+n), and/or A(x,y) and B(x+n,y), where n is any number.

    If you create a table for testing, with a mix of horizontally-merged and vertically-merged cells, you can use the following VBA macro to get each cell's address - the address is displayed on Word's status bar. As you will see, with horizontally-merged cells, then column index for the next cell on the same row only increases by 1 but the row index for the next cell in the column with vertically-merged cells increases by the number of merged cells.

    Sub CellAddress()
    'This macro displays the address of a table cell on Word's status bar
    If Selection.Information(wdWithInTable) = True Then
      If Selection.Cells(1).ColumnIndex > 26 Then
        StatusBar = "Cell Address: " & Chr(64 + Int(Selection.Cells(1).ColumnIndex / 26)) & _
        Chr(64 + (Selection.Cells(1).ColumnIndex Mod 26)) & Selection.Cells(1).RowIndex
      Else
        StatusBar = "Cell Address: " & Chr(64 + Selection.Cells(1).ColumnIndex) & _
        Selection.Cells(1).RowIndex
      End If
    End If
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by zzGuiJi Friday, January 23, 2015 5:08 AM
    Friday, January 23, 2015 4:09 AM
  • Thank you vary much.

    I will parse the cell by myself.Yesterday i try every property of table , row , column , range and cell to find the cue.

    Now I know how to do. Thank you!

    Friday, January 23, 2015 5:22 AM