0
votes

My Word-table has some splitted cells in a header. I try to insert data to lower cells. But I have Issue that it works in some Pcs and crash on others message "Cannot access individual columns in this collection because the table has mixed cell width"Stack Trace:at Microsoft.Office.Interop.Word.Cell.get_Column()" How to work with a table of such kind? Thanks.

Here's the basic setup of the Word doc in the code:

    #region Create  table -->Body


            Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
            //nRows =5;//Number of rows
            int nRows = cats.Count + 1;
            Table firstTable = document.Tables.Add(para1.Range, nRows, 3, ref missing, ref missing);
            #region Style

            // object setStyle = Microsoft.Office.Interop.Word.WdTableFormat.wdTableFormatProfessional;
            object setStyle = "Table Professional ";
            firstTable.set_Style(ref setStyle);


          // object styleName = "Table Professional";
         //   object styleName = Microsoft.Office.Interop.Word.WdTableFormat.wdTableFormatProfessional;
           // firstTable.set_Style(ref styleName);
            int tcount = 0;
            bool isfirstCellInRow = false;
            firstTable.Borders.Enable = 1;
            firstTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleEngrave3D;
            firstTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleEmboss3D;
            firstTable.TableDirection = Microsoft.Office.Interop.Word.WdTableDirection.wdTableDirectionRtl;
            firstTable.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdAuto;
    //       firstTable.PreferredWidth = Microsoft.Office.Interop.Word.;
            #endregion

            int rowcount = -1;

            foreach (Row row in firstTable.Rows)
            {
                isfirstCellInRow = true;

                foreach (Cell cell in row.Cells)
                {
                    cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                    #region Fill Header ^_^
                    if (cell.RowIndex == 1)
                    {
                        //------------------------------- Set Header 
                        if (cell.RowIndex == 1 && cell.ColumnIndex == 1)
                        {
                            cell.Range.Text = "م";
                           //   cell.Column.SetWidth(33f, WdRulerStyle.wdAdjustNone);
                        }
                        else if (cell.RowIndex == 1 && cell.ColumnIndex == 2)
                        {
                            cell.Range.Text = " العميل ";
                        }
                        else if (cell.RowIndex == 1 && cell.ColumnIndex == 3)
                        {
                            cell.Range.Text = "المنطقه ";
                        }

                        cell.Range.Font.Bold = 1;
                        //other format properties goes here
                        cell.Range.Font.Name = "verdana";
                        cell.Range.Font.Size = 10;
                        //cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;                            
                        cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
                        //Center alignment for the Header cells
                        cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                        cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

                    }
                    #endregion
                    //Data row
                    else
                    {
                        //if (isfirstCellInRow)
                        //{
                        //    cell.Range.Text = (rowcount + 1).ToString(); //(cell.RowIndex - 2 + cell.ColumnIndex).ToString();
                        //}
                        //else
                            switch (cell.ColumnIndex)
                            {
                                case 1:
                                    cell.Range.Text = (rowcount + 1).ToString(); //(cell.RowIndex - 2 + cell.ColumnIndex).ToString();
                                    break;
                                case 2:
                                    cell.Range.Text = cats[rowcount].Name;
                                    break;
                                case 3:
                                    cell.Range.Text = cats[rowcount].SubDistricts;
                                    break;
                                default:
                                    break;
                            }
                    }

                    isfirstCellInRow = false;
                }
                rowcount++;
                tcount++;
            }
            #endregion
1

1 Answers

0
votes

Just need to Add Line Before Table As there is another table above this one so it was creates the tables with out any Space Between solved By Adding this code before the table

     Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
            Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);