Office developer documentation, downloads, tools | Office 365, Apps for Office, Apps for SharePoint

Office Developer Documentation

Office 365 APIs

Access Office 365 data for websites or native applications running on iOS, Android, Windows, and other device platforms with the Office 365 REST APIs.

APIs

Develop for

APIs for

Try the Office 365 APIs

Office Add-ins (formerly Apps for Office)

Build an add-in that lives inside Outlook, Excel, Word, PowerPoint, or Project to extend what you can do from right in the documents, across devices and platforms — like pulling content from the web, calling mapping services, locating news, translating text, and more.

SharePoint Add-ins (formerly Apps for SharePoint)

Build SharePoint-hosted or provider-hosted add-ins to use remote web applications, surface remote data, offload business logic, access SharePoint list data, integrate with Office applications and services, make SharePoint a portal for other applications, and more.

 none
How to merge cells in table in word document ?

    السؤال

  • Hello,

    I'm trying to merge table using c#, I tried several code but no use...

    I Tried

    ----------------

    table.Cell(rowIndex, coloumnIndex).Range.Select();
    oWord.Selection.MoveUp(ref moveUnit, ref moveCount, ref moveExtend);
    oWord.Selection.Cells.Merge();

     

    it generated error when there was cell in a row that contained many data
    ---------------

    Then I tried:

     object startLocation = table.Cell(startIndex, coloumnIndex);
     object endLocation = table.Cell(endIndex, coloumnIndex);
     Word.Range mergeRange = oDoc.Range(ref startLocation, ref endLocation);
     mergeRange.Select();
     mergeRange.Cells.Merge();

    It did nothing...

    ---------------

    Finally I tried

     table.Rows[endIndex].Cells[1].Merge(table.Rows[startIndex].Cells[1]);

    it merged 1st column, but when i tried to merge the other columns it said "Cannot access individual rows in this collection because the table has vertically merged cells."

    --------------

     

    any help... ?

     

    Thanks in advance...

    15/محرم/1432 02:22 م

الإجابات

  • Hi 3y3y

    OK, here's some sample code (in VBA, because that's fastest to test) that bascially does what I understand you need. I don't think you should have any problems converting this to C#.

    Basically, what it does is get the number of rows and divide by 4 (the number of cells to be merged vertically). This is the outside loop. The inside loop is the number of columns.

    Since Word doesn't really like to work with row/column indexes when it contains merged cells, about the only way you can do this is to "walk" the cells. I'm using the Cell object, rather than the Cells object because that allows me to set the size of cell range I want to merge (MergeTo parameter).

    Sub Merge4RowsPerCol()
      Dim tbl As word.Table
      Dim cel As word.Cell
      Dim rowIndex As Long, colIndex As Long, i As Long, r As Long
      
      Set tbl = ActiveDocument.Tables(1)
      colIndex = tbl.Columns.Count
      rowIndex = tbl.Rows.Count / 4
      
      For r = 1 To rowIndex
        For i = 1 To colIndex
          Set cel = tbl.Cell(r, i)
          cel.Merge MergeTo:=tbl.Cell(cel.rowIndex + 3, i)
        Next i
      Next r
    End Sub
    
    

    Cindy Meister, VSTO/Word MVP
    • تم وضع علامة كإجابة بواسطة 3y3y 17/محرم/1432 11:46 ص
    17/محرم/1432 09:59 ص
    المشرف
  • Thanksssssssss Cindy

    It worked really appreciated :)

    anyway I'll put the code for anyone who might face same problem

    private void MergeTable(Word.Table table)
        {
          Word.Cell cell;
          int rowIndex = 2;                //Starting of 2 row to ignore first row
          int blockIndex = (table.Rows.Count - 1) / 4;  //Block of 4 rows to be merged and -1 to ignore first row in the table
          int coloumnIndex = 4;              //Selecting first 4 coloums
    
          for (int blockCounter = 1; blockCounter < blockIndex; blockCounter++)
          {
            for (int colCounter = 1; colCounter <= coloumnIndex; colCounter++)
            {
              cell = table.Cell(rowIndex, colCounter);
              cell.Merge(table.Cell(cell.RowIndex + 3, colCounter));
            }
            rowIndex += 4;				  //Updated rowIndex to the start of the next 4 rows
          }
        }
    

    • تم وضع علامة كإجابة بواسطة 3y3y 20/محرم/1432 06:40 ص
    17/محرم/1432 12:06 م

جميع الردود

  • Hi 3y3y

    Yeah, merging cells in Word tables can be a real pain...

    First thing to check: are you able to merge the cells you want to merge as an end-user? If Word won't let you do it as an end-user, then you won't be able to do it in the object model.

    If you can do it as an end-user, then there's a chance it can be done in the object model, we just have to find the way (and it may not be "pretty").

    Also, which version of Word are you working with?


    Cindy Meister, VSTO/Word MVP
    15/محرم/1432 06:58 م
    المشرف
  • Hi Cindy Meister,

    Yes I'm able to merge cells as an end-user, and I'm using dll Microsoft.Office.Interop.Word version 11.0.0.0 to create word 2003 document.

     

    I'm having table with many rows and 4 columns

    I'm supposed to merge 4 rows in a table in each column, then next 4 rows and so one...

     

    object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
    object moveCount = 3;
    object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
    int rowIndex = 5;
    
    //Vertical Merge
    for (int coloumnIndex = 4; coloumnIndex >= 1; coloumnIndex--)
     {
      try //To get the first row when there are no merged cells in the tables
        {
         table.Cell(rowIndex, coloumnIndex).Range.Select();
         oWord.Selection.MoveUp(ref moveUnit, ref moveCount, ref moveExtend);
         oWord.Selection.Cells.Merge();
        }
    
       catch
         {
                        
         }
     }
     rowIndex += 4;

    if you can help me, I'll be greatly appreciated :)

     

    Thanks in advance.

     

    17/محرم/1432 09:16 ص
  • Hi 3y3y

    OK, here's some sample code (in VBA, because that's fastest to test) that bascially does what I understand you need. I don't think you should have any problems converting this to C#.

    Basically, what it does is get the number of rows and divide by 4 (the number of cells to be merged vertically). This is the outside loop. The inside loop is the number of columns.

    Since Word doesn't really like to work with row/column indexes when it contains merged cells, about the only way you can do this is to "walk" the cells. I'm using the Cell object, rather than the Cells object because that allows me to set the size of cell range I want to merge (MergeTo parameter).

    Sub Merge4RowsPerCol()
      Dim tbl As word.Table
      Dim cel As word.Cell
      Dim rowIndex As Long, colIndex As Long, i As Long, r As Long
      
      Set tbl = ActiveDocument.Tables(1)
      colIndex = tbl.Columns.Count
      rowIndex = tbl.Rows.Count / 4
      
      For r = 1 To rowIndex
        For i = 1 To colIndex
          Set cel = tbl.Cell(r, i)
          cel.Merge MergeTo:=tbl.Cell(cel.rowIndex + 3, i)
        Next i
      Next r
    End Sub
    
    

    Cindy Meister, VSTO/Word MVP
    • تم وضع علامة كإجابة بواسطة 3y3y 17/محرم/1432 11:46 ص
    17/محرم/1432 09:59 ص
    المشرف
  • Thanksssssssss Cindy

    It worked really appreciated :)

    anyway I'll put the code for anyone who might face same problem

    private void MergeTable(Word.Table table)
        {
          Word.Cell cell;
          int rowIndex = 2;                //Starting of 2 row to ignore first row
          int blockIndex = (table.Rows.Count - 1) / 4;  //Block of 4 rows to be merged and -1 to ignore first row in the table
          int coloumnIndex = 4;              //Selecting first 4 coloums
    
          for (int blockCounter = 1; blockCounter < blockIndex; blockCounter++)
          {
            for (int colCounter = 1; colCounter <= coloumnIndex; colCounter++)
            {
              cell = table.Cell(rowIndex, colCounter);
              cell.Merge(table.Cell(cell.RowIndex + 3, colCounter));
            }
            rowIndex += 4;				  //Updated rowIndex to the start of the next 4 rows
          }
        }
    

    • تم وضع علامة كإجابة بواسطة 3y3y 20/محرم/1432 06:40 ص
    17/محرم/1432 12:06 م
Office developer documentation, downloads, tools | Office 365, Apps for Office, Apps for SharePoint

Office Developer Documentation

Office 365 APIs

Access Office 365 data for websites or native applications running on iOS, Android, Windows, and other device platforms with the Office 365 REST APIs.

APIs

Develop for

APIs for

Try the Office 365 APIs

Office Add-ins (formerly Apps for Office)

Build an add-in that lives inside Outlook, Excel, Word, PowerPoint, or Project to extend what you can do from right in the documents, across devices and platforms — like pulling content from the web, calling mapping services, locating news, translating text, and more.

SharePoint Add-ins (formerly Apps for SharePoint)

Build SharePoint-hosted or provider-hosted add-ins to use remote web applications, surface remote data, offload business logic, access SharePoint list data, integrate with Office applications and services, make SharePoint a portal for other applications, and more.