2
votes

I'm trying to put a Microsoft.Office.Interop.Word.Shape inside a table cell like in this short example:

Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Word.Document oDocument = oWord.Documents.Add();
Word.Table oTable = oDocument.Tables.Add(oDocument.Range(), 4, 1);

Word.Cell oCell1 = oTable.Cell(1,1);
Word.Shape oShape1 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell1.Range);

Word.Cell oCell2 = oTable.Cell(2, 1);
Word.Shape oShape2 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell2.Range);

Word.Cell oCell3 = oTable.Cell(3, 1);
Word.Shape oShape3 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell3.Range);

Word.Cell oCell4 = oTable.Cell(4, 1);
Word.Shape oShape4 = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, oCell4.Range);

  oWord.Visible = true;

The rectangle only appears on the top left corner of the document. I'm not sure what I'm doing wrong since I set the shape anchor to the cells range.

12/07/2016:

Okay, check this out, I have now 5 columns and 100 rows and try to place the shape into the third column. I'm using the "in line with text" property.

Now, on the first two pages, all the shapes are placed in the first row/column. Starting at the third page it's looking right...

I'm using Office 2013.

Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Word.Document oDocument = oWord.Documents.Add();

int numRows = 100;
Word.Table oTable = oDocument.Tables.Add(oDocument.Range(), numRows, 5);
oTable.Borders.OutsideLineStyle =  Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
oTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

for (int r = 1; r <= numRows; ++r)
{
    Word.Range anchorRange = oTable.Cell(r, 3).Range;
    Word.Shape oShape = oDocument.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle.GetHashCode(), 7, 7, 11, 11, anchorRange);
    oShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapInline;
}
oWord.Visible = true;
1

1 Answers

0
votes

If you click on the rectangle that's added, and enable paragraph/formatting symbols (it's the backwards P looking button in Home > Paragraph), you'll notice that the anchors are indeed on the table cells. Text-wrapped shapes also need other settings to have certain positions on the page. Mess with the Size and Position dialog to get a feel for it.