Home | Office Dev Center | Office, SharePoint, Apps for Office and SharePoint, Office 365 APIs

Office 365 APIs Preview

Office 365 APIs extend the Office 2013 platform through REST APIs and standard OAuth flows so that websites and native applications running on Windows 8, iOS, Android, and other device platforms can consume Office 365 data.

Office 365 APIs Preview graphic

It's much simpler to write code for the Office apps store because we get to use the skills we already have being web native developers. And that means we can reach every version of Office from here on out with the same code that we already write for all major web browers.

- Poll Everywhere, Office 365 Partner

Apps for Office

An app for Office is a webpage you build and host inside an Office client application to extend what your document, spreadsheet, or email message can do.

Run them in:

  • Office desktop clients
  • Office Online
  • Mobile browsers

Apps for SharePoint

Apps for SharePoint are easy-to-use, lightweight web applications that extend what your SharePoint website can do.

Use them as:

  • Immersive full-page experiences
  • Parts of a web page
  • UI custom actions

Opportunity

Reach 1 billion + worldwide Office
users.

Learn more Right arrow in circle

Transform

Use skills you know and familiar tools
to transform your solutions.

Get started Right arrow in circle

Support

Talk to other devs, tell your app story,
and get help.

Get connected Right arrow in circle

 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 م
Home | Office Dev Center | Office, SharePoint, Apps for Office and SharePoint, Office 365 APIs

Office 365 APIs Preview

Office 365 APIs extend the Office 2013 platform through REST APIs and standard OAuth flows so that websites and native applications running on Windows 8, iOS, Android, and other device platforms can consume Office 365 data.

Office 365 APIs Preview graphic

It's much simpler to write code for the Office apps store because we get to use the skills we already have being web native developers. And that means we can reach every version of Office from here on out with the same code that we already write for all major web browers.

- Poll Everywhere, Office 365 Partner

Apps for Office

An app for Office is a webpage you build and host inside an Office client application to extend what your document, spreadsheet, or email message can do.

Run them in:

  • Office desktop clients
  • Office Online
  • Mobile browsers

Apps for SharePoint

Apps for SharePoint are easy-to-use, lightweight web applications that extend what your SharePoint website can do.

Use them as:

  • Immersive full-page experiences
  • Parts of a web page
  • UI custom actions

Opportunity

Reach 1 billion + worldwide Office
users.

Learn more Right arrow in circle

Transform

Use skills you know and familiar tools
to transform your solutions.

Get started Right arrow in circle

Support

Talk to other devs, tell your app story,
and get help.

Get connected Right arrow in circle