none
C# interop on Word 2016: insert shape, rotate 90, wrap inline. RRS feed

  • Question

  • Hi,

    I need help with the following behaviors that I find strange ...

    Using c# and interop,  I need to find a piece of text in the Word document and replace it with an image. In some cases, I need to rotate the image and it should wrap inline, not cover text.

    I've read some of the articles on these forums and stackoverflow but for some reason I'm having some trouble.

    Rotate on demand/insert does not work, no matter what

    The code I started with:

    // range is found earlier via search InlineShape aShape = range.InlineShapes.AddPicture(@"c:\test.png", Type.Missing, Type.Missing, Type.Missing); aShape.ConvertToShape(); aShape.Rotation = 90;

    The code above does insert the image but it is not rotated. shape.IncrementRotation(90) does not work either.

    It also does not matter what the range is. I tried directly on document.Content

    Another version

    // this does not work either
    Shape testShape = theDoc.Shapes.AddPicture(@"c:\test.png", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, range);
    testShape.IncrementRotation(90);
    

    Rotate later works, kind of

    By later, I don't mean later in the same execution, I mean reopen the document, look for shapes and rotate

                foreach (Microsoft.Office.Interop.Word.Shape shape in wordDocument.Content.ShapeRange)
                {
                    shape.IncrementRotation(90);
                }
    


    The above will get the shape rotated finally, but I really don't want to have to reopen it. To make things worse, if I add in the same loop the code to make it inline, the shape is not rotated (i.e. shape.WrapFormat.Type = WdWrapType.wdWrapInline).  I need to have another loop.

    Anyway, to recap, any idea why I can't rotate a shape as soon as I insert it.

    Thank you

    Cristian

    Sunday, August 27, 2017 11:11 AM

All replies

  • Hi CCurelaru,

    ->aShape.Rotation = 90;

    aShape is InlineShape object and InlineShape does not have the Shape.Rotation Property. ConvertToShape will return a shape object and you need use the returned shape object to set rotation property.

    Here is the example.

    string imagePath = @"C:\Users\v-guaxu\Desktop\Picture\pic.jpg";
    
                Word.InlineShape iShape = range.InlineShapes.AddPicture(imagePath, Type.Missing, Type.Missing, Type.Missing);
    
                Word.Shape ashape = iShape.ConvertToShape();
    
                ashape.Rotation = 90;

    Best Regards,

    Terry

    Monday, August 28, 2017 6:17 AM
  • Hi Terry,

    (don't know why I can't use anymore ccurelaru but it's me)

    Right, I get your point. My example was not correct. I edited a bit the lines of code for this post but in the actual code it was basically one line:

    aShape.ConvertToShape().Rotation = 90; 


    My question to you is - have you tried it and it works? If yes, what version of MS Word you have ?

    thanks

    Cristian


    • Edited by CristianCA Monday, August 28, 2017 7:44 PM
    Monday, August 28, 2017 3:02 PM
  • Hi CristianCA,

    The code works for me to rotate the picture too. I'm working on Word 2016. It seems you want to keep the picture wrap inline, right? I think you could convert the shape to inline shape after rotating.

    Here is the example.

    Word.Shape ashape = iShape.ConvertToShape();
    
                ashape.Rotation = 90;
    
                ashape.ConvertToInlineShape();

    Best Regards,

    Terry

    Tuesday, August 29, 2017 1:57 AM
  • ok, thanks. I'll maybe try it on a standalone program as for me it's not being rotated for some reason.
    Tuesday, August 29, 2017 5:57 PM
  • Hi CCurelaru,

    What's your current issue? Has your original issue has been solved, if it has, I would suggest you mark useful reply or provide your solution and then mark it to help other developers use this forum efficiently. If not, please feel free to let us know your current issue.

    Thanks for understanding.

    Best Regards,

    Terry

    Friday, September 1, 2017 5:42 AM