Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a word documant that contains text box shapes.
I'm tring to create a MS Word table inside one of this text box shapes.
But I'm having problem with the range setings.
(Microsoft.Office.Interop.Word.Range wordRange = wordDoc.Shapes[wordShapeIndexes[id]].TextFrame.ContainingRange;
throws exeption - The index into the specified collection is out of bounds.), can some one help me please ?

Here is my function:
C#
public void setWordShapeDataTask(int id, DataGridView data)
      {
          int tableRowNumber = data.RowCount + 1;
          Microsoft.Office.Interop.Word.Table wordTable;
          Microsoft.Office.Interop.Word.Range wordRange =  wordDoc.Shapes[wordShapeIndexes[id]].TextFrame.ContainingRange;

          wordTable = wordDoc.Tables.Add(wordRange, tableRowNumber, 5);

          wordTable.Cell(0, 0).Range.Text = "Num";
          wordTable.Cell(0, 1).Range.Text = "Task";
          wordTable.Cell(0, 2).Range.Text = "Recipiens";
          wordTable.Cell(0, 3).Range.Text = "Time";
          wordTable.Cell(0, 4).Range.Text = "Status";

          for (int i = 1, j = 0; i < tableRowNumber; i++, j++)
          {
              wordTable.Cell(i, 0).Range.Text = i.ToString();
              wordTable.Cell(i, 1).Range.Text = data.Rows[j].Cells["TaskBody"].Value.ToString();
              wordTable.Cell(i, 2).Range.Text = data.Rows[j].Cells["Recipiens"].Value.ToString();
              wordTable.Cell(i, 3).Range.Text = data.Rows[j].Cells["DueDate"].Value.ToString();
              wordTable.Cell(i, 4).Range.Text = data.Rows[j].Cells["Status"].Value.ToString();
          }
      }
Posted
Updated 14-Dec-17 2:39am
v4

C#
public void setWordShapeDataTask(int id, DataGridView data)
        {
            try
            {
                int tableRowNumber = data.RowCount + 1;
                Microsoft.Office.Interop.Word.Table wordTable;
                foreach (Microsoft.Office.Interop.Word.Shape shape in wordDoc.Shapes)
                    if (shape.ID == wordShapeIndexes[id])
                    {
                        Microsoft.Office.Interop.Word.Range wordRange = shape.TextFrame.ContainingRange;

                        wordTable = wordDoc.Tables.Add(wordRange, tableRowNumber, 5);
                        //wordTable = wordDoc.Tables.Add(wordRange, 4, 5);
                        wordTable.Range.Font.Size = 8;
                        wordTable.set_Style("Table Grid 8");

                        wordTable.Cell(1, 1).Range.Text = "Num";
                        wordTable.Cell(1, 2).Range.Text = "Task";
                        wordTable.Cell(1, 3).Range.Text = "Recipiens";
                        wordTable.Cell(1, 4).Range.Text = "Time";
                        wordTable.Cell(1, 5).Range.Text = "Status";

                        for (int i = 2, j = 0; i <= tableRowNumber; i++, j++)
                        {
                            wordTable.Cell(i, 1).Range.Text = (i-1).ToString();
                            wordTable.Cell(i, 2).Range.Text = data.Rows[j].Cells["TaskBody"].Value.ToString();
                            wordTable.Cell(i, 3).Range.Text = data.Rows[j].Cells["Recipiens"].Value.ToString();
                            wordTable.Cell(i, 4).Range.Text = data.Rows[j].Cells["DueDate"].Value.ToString();
                            wordTable.Cell(i, 5).Range.Text = data.Rows[j].Cells["Status"].Value.ToString();
                        }
                    }
            }
            catch (Exception)
            {
            }

        }
 
Share this answer
 
v3
Looks like you are looping from index 1 to tableRowNumber for data rows. It should start from 0 till rowCount.
Try:
C#
// with int tableRowNumber = data.RowCount + 1;
for (int i = 0; i < tableRowNumber-1; i++)
 
Share this answer
 
v2
Comments
Sandeep Mewara 6-Jan-13 4:35am    
Even then, following will throw index error:
data.Rows[i].Cells when i=tableRowNumber highest value.
As tableRowNumber = rowCount+1 ... thus, you try to access a row at index that does not exists.

Start from 1 for i but go till 1 less of tableRowNumber. Ex:
for (int i = 1; i < tableRowNumber-1; i++)
supernatik 6-Jan-13 4:38am    
Thank you for your answer, I fixed the i = 0 issue, added j.
But My original problem is creating the table inside the text box shape.
I having an exeption with the range.

Microsoft.Office.Interop.Word.Range wordRange = wordDoc.Shapes[wordShapeIndexes[id]].TextFrame.ContainingRange;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900