Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
  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 5-Jan-13 21:53pm
Edited 6-Jan-13 0:48am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
            {
            }
 
        }
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Looks like you are looping from index 1 to tableRowNumber for data rows. It should start from 0 till rowCount.
Try:
  
// with int tableRowNumber = data.RowCount + 1;
for (int i = 0; i < tableRowNumber-1; i++)
  Permalink  
v2
Comments
Sandeep Mewara at 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 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100