Cannot insert shape inside table cell RRS feed

  • Question

  • I have searched all across the Internet and I have not found a solution to my problem.

    I am using Word Interop to create Word documents. I have a table and I want to insert some circular shapes into some specific cells. I have read the documentation from the MSDN and I have come down to this code to insert a circular shape with specific color background in cell (2,5) of my table. 

    Word.Shape textBox = wordDocument.Shapes.AddShape(Core.MsoAutoShapeType.msoShapeOval.GetHashCode(), 0, 0, 19.5f, 19.5f, testsTable.Cell(2,5).Range);

    textBox.Fill.ForeColor.RGB = Color.FromArgb(0, 250, 0).ToArgb();

    However, the shape insists on being drawn at the start of  the page [ (0,0) position of the page ].

    Is there any hint on this? 

    Thank you in advance.

    Tuesday, June 27, 2017 2:16 PM

All replies

  • As far as I can tell, when you use AddShape to add to a .docx, Word ignores the Range parameter but uses the Left and Top parameters to decide where to place the anchor.

    However, that means that you end up relying on a heuristic to position the anchor in the correct place. For example in a simple situation in VBA you might use something like

    Dim r as Word.Range
    Dim s As Word.Shape
    Set r = testsTable.Cell(2,5).Range
    Set s = wordDocument.Shapes.AddShape(MsoAutoShapeType.msoShapeOval, r.Information(wdHorizontalPositionRelativeToPage), r.Information(wdVerticalPositionRelativeToPage) + 1, 19.5,19.5, r)

    The "+1" is there to try to prevent Word from anchoring the shape to the previous paragraph. I suspect there are scenarios in which that adjustment is inadequate.

    Then modify your shape as required. If you actually want the shape to appear inside the cell, it's probably simplest to convert it to an InlineShape:


    The current MSDN documentation is not particularly helpful. The documentation for the Shapes collection suggests that you can specify a range when you add one of the various kinds of shape, and that Word will default to anchoring the shape to the first paragraph on the page if you do not specify the Anchor range (or something like that). But the documentation for the individual "AddShape" type routines omits the Range parameter altogether, whereas Word's own VBA Object Browser shows the Range parameter as optional.

    As far as I can tell, the reality is that Word's behaviour actually depends on the document format you are working with. If you save a document as .doc, then run code like yours with no Left and Top, but with a Range parameter, Word honours the Range parameter. In fact, it does that even if you save the document as a .doc, *then* save it as a .docx and work with that, which suggests that there is another compatibility mode issue of some kind. But in that case, if you attempt to use the s.ConvertInlineShape method, it fails.  

    A completely different approach might be to use the Selection, i.e. insert the shape and get a reference to it, select it, then cut it and paste it into the correct range.

    Peter Jamieson

    Wednesday, June 28, 2017 12:24 PM