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

Office Dev Center

Introducing Office 365 APIs Preview


Use single sign-on (SSO) for Office resources

Connect using REST and OAuth

Choose new APIs

The preview release of our new Office 365 APIs extend the platform that we introduced in Office 2013 to support the ability for both websites and native applications to consume Office 365 data.

Office 365 APIs Preview graphic

The story of apps


What are apps for SharePoint?

Apps are small, easy-to-use, stand-alone applications that solve a need for your users or business.

Site owners can install, upgrade, and uninstall them on their SharePoint sites without farm or site collection admins.

What are apps for Office?

Apps are web applications that extend what a document, email message, or meeting request can do in Office.

Run them in multiple environments like Office desktop applications, Office Online, and mobile browsers—in the cloud or on your site.

Why create apps


Build a new class of solutions with the App Model

Extend the way you create and use information from Office and SharePoint.

Deploy and maintain apps faster in the cloud

Use any hosting services to run and deploy them.

Use skills you have and tools you already know

HTML/CSS, JavaScript, PHP, or .NET, and web dev tools like Visual Studio.

Publish apps to your own app catalog

Or sell them in the Office Store.

 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 Dev Center

Introducing Office 365 APIs Preview


Use single sign-on (SSO) for Office resources

Connect using REST and OAuth

Choose new APIs

The preview release of our new Office 365 APIs extend the platform that we introduced in Office 2013 to support the ability for both websites and native applications to consume Office 365 data.

Office 365 APIs Preview graphic

The story of apps


What are apps for SharePoint?

Apps are small, easy-to-use, stand-alone applications that solve a need for your users or business.

Site owners can install, upgrade, and uninstall them on their SharePoint sites without farm or site collection admins.

What are apps for Office?

Apps are web applications that extend what a document, email message, or meeting request can do in Office.

Run them in multiple environments like Office desktop applications, Office Online, and mobile browsers—in the cloud or on your site.

Why create apps


Build a new class of solutions with the App Model

Extend the way you create and use information from Office and SharePoint.

Deploy and maintain apps faster in the cloud

Use any hosting services to run and deploy them.

Use skills you have and tools you already know

HTML/CSS, JavaScript, PHP, or .NET, and web dev tools like Visual Studio.

Publish apps to your own app catalog

Or sell them in the Office Store.