none
VSTO Word - inlineShape.ConvertToShape in contentControls problem RRS feed

  • Question

  • Hi everyone,

    I'm at the beginning of vsto development in c# .. and so i have a question about inlineshapes in content controls.

    I add a pictureContentControl to the controls of my activedocument and after this I add a picture:

    ------------------------------------------------------------------------------------------------------------

    var range = Globals.ThisAddIn.Application.Selection.Range;
    var contentControl = vstoDocument.Controls.AddPictureContentControl(range, $"CHART{Guid.NewGuid()}");
    contentControl.LockContents = false;
    contentControl.Title = chartContentControl.Title;
    contentControl.Tag = chartContentControl.Key;
    contentControl.Range.InlineShapes.AddPicture(imagePath);

    // Up to here it runs well!

    var myInlineShape = contentControl.Range.InlineShapes.Cast<InlineShape>()
                                            .Where(inlineShape => inlineShape.Type == WdInlineShapeType.wdInlineShapePicture)
                                            .First();

    //Now i want to convert the InlineShape to a shape

    var convertedShape = myInlineShape.ConvertToShape(); // Exception occurs here

    //now i want to get the rotation of the control, but the conversion failed in the previous step

    var rotation = convertedShape.Rotation;

    --> Exception:

    {"Error HRESULT E_FAIL has been returned from a call to a COM component."}

     ErrorCode = -2147467259

    ---------------------------------------------------------------------------------

    Does anybody know why the conversion always fails or is there an other way to rotate pictures in inlineshapes?

    Thanks in advance for answers! 

    Philipp

    Wednesday, June 7, 2017 2:01 PM

Answers

  • Hi,

    I wanted to inform you about my solution/walkaround for the issue:

    I rotated the image itself and after this I saved this image in my local "C:\Temp" path.

    Further on i created a new PictureContentControl and added the rotated image from the local path.

    var rotatedEmfImage = Image.FromFile(emfImagePath);
    rotatedEmfImage.RotateFlipRotateFlipType.Rotate270FlipNone);
    var rotatedEmfName = $"Image{Guid.NewGuid()}.emf";
    var rotatedEmfPath = Path.GetTempPath() + rotatedEmfName;
     rotatedEmfImage.Save(rotatedEmfPath, ImageFormat.Emf);
     rotatedEmfImage.Dispose();
    
    var picGuid = Guid.NewGuid();
    var pictureContentControl = document.Controls.AddPictureContentControl(nativeControl, $"CHART{picGuid}");
    pictureContentControl.LockContents = false;
    pictureContentControl.Title = chartContentControl.ChartInfo.Title;
    pictureContentControl.Tag = chartContentControl.Key;
    
    var insertedShape = pictureContentControl.Range.InlineShapes.AddPicture(rotatedEmfPath); 
    

    With this approach, I could meet the requirement as far as possible.

    Best regards,

    Philipp

    • Marked as answer by Philipp Fo Tuesday, June 27, 2017 6:46 AM
    Tuesday, June 27, 2017 6:46 AM

All replies

  • Hi Philipp Fo,

    try to create object of 'shape' and then try to convert to shape.

    do not use 'var'.

    then to rotate the image you need to use 'increment rotation'.

    check line below also work.

     this.Application.ActiveDocument.InlineShapes[1].ConvertToShape().IncrementRotation(90);

    you cannot store shape in 'var'.

    same you need to change with other 'var'.

    you need to create object that can hold the shape.

    Reference:

    InlineShape.ConvertToShape Method (Word)

    Shape.IncrementRotation Method (Word)

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 8, 2017 7:12 AM
    Moderator
  • Hi Deepak,

    Thanks for the fast reply!

    I tried to use your suggestion to solve my problem, but this didn't work...

    Microsoft.Office.Interop.Word.Shape myShape = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes[1].ConvertToShape();

    Or...

    Globals.ThisAddIn.Application.ActiveDocument.InlineShapes[1].ConvertToShape().IncrementRotation(90);

    Occurred the same exception as before...  

    Are there any other ideas?

    Kindest regards,

    Philipp

    Friday, June 9, 2017 6:46 AM
  • Hi Philipp Fo,

    I try to again test the code and I find that the same code is working correctly on my side.

    please check below testing result.

    second test with second code:

    you can see that I use the same code that you had posted and it convert to the shape and rotate it successfully.

    I suggest you to create a new addin for testing purpose and test the code again.

    let me know about your testing result, so that I can try to provide you further suggestion.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, June 9, 2017 7:04 AM
    Moderator
  • Hi Deepak,

    I tested it with this suggestion:

    Code from the startup method:

    var tempPath = Path.GetTempPath() + "\\myImage.emf";
    
    //this works with the button click in the ribbon:
    
    //var inlineShape = Globals.ThisAddIn.Application.ActiveDocument.InlineShapes.AddPicture(tempPath);
    //inlineShape.Title = "InlineShape";
    
    //this does not work
    
    var vstoDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
                var range = Globals.ThisAddIn.Application.Selection.Range;
                var contentControl = vstoDocument.Controls.AddPictureContentControl(range, $"CHART{Guid.NewGuid()}");
                contentControl.LockContents = false;
                contentControl.Title = "contentControlTest";
                contentControl.Tag = "contentControlKey";
                contentControl.Range.InlineShapes.AddPicture(tempPath);


    The problem is that my Inlineshape is in the range of the contentControl.

    Code from the buttonClick method in the ribbon:

                //works
                //for (int i = 1; i < Globals.ThisAddIn.Application.ActiveDocument.InlineShapes.Count + 1; i++)
                //{
                //    Globals.ThisAddIn.Application.ActiveDocument.InlineShapes[i].ConvertToShape().IncrementRotation(90);
                //}
    
                //exception occured
                for (int i = 1; i < Globals.ThisAddIn.Application.ActiveDocument.ContentControls.Count + 1; i++)
                {
                    var control = Globals.ThisAddIn.Application.ActiveDocument.ContentControls[i];
                    var inlineShape = control.Range.InlineShapes[1];
                    inlineShape.ConvertToShape().IncrementRotation(90);
                }

    Maybe you can test this in your solution? Thanks!

    Regards,

    Philipp

    Friday, June 9, 2017 8:19 AM
  • Hi Philipp Fo,

    I try to check it on my side and find that when we don't use content control then code is working.
    when we use content control it generates the error.

    I will try to make a further test to check whether we can convert the shape in content control or not.

    if you just want to rotate the image in that content control then did you try to rotate the image content control it self?

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 9, 2017 9:52 AM
    Moderator
  • Hi Deepak,

    the requirement is:

    Insert a contentcontrol to a certain place in the word document. Then the user can rotate the image in the contentcontrol. After this, the rotated picture in the contentcontrol has to be replaced by another picture with the rotation of the old picture.

    So I have to note the rotation of the old picture in the content control, to give the new picture the required rotation.

    Do you understand the issue?

    Regards,

    Philipp

    Friday, June 9, 2017 11:04 AM
  • Hi Philipp Fo,

    I try to check content controls properties and methods.

    ContentControl Object (Word)

    I did not get any property or method that can rotate the image in the content control.

    you can also check the link above to check it's property and methods.

    I will try to search further and if I get anything useful , I will try to provide you.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 12, 2017 8:35 AM
    Moderator
  • Hi Philipp,

    Could you share us the reason you add an image inlineshape to PictureContentControl?

    I suggest you try to check whether below code meet your requirement.

                var imagePath = @"C:\Users\xx\Desktop\img.jpg";
                var vstoDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
                var range = Globals.ThisAddIn.Application.Selection.Range;
                Word.InlineShape inlineShape = range.InlineShapes.AddPicture(imagePath);
                Word.Shape shape = inlineShape.ConvertToShape();
                shape.Rotation = 45;
                MessageBox.Show(shape.Rotation.ToString());

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 16, 2017 7:56 AM
  • Hi Edward,

    thanks for your reply!

    Honestly I have the same workaround for my issue...

    But this way does not meet the requirement.

    I want to use a contentcontrol with the converted and rotated shape in it.

    I still hope for a solution..Best Regards,

    Philipp

    Friday, June 16, 2017 12:44 PM
  • Hi Philipp Fo,

    I try to search further and make some tests on my side.

    but still not able to rotate the picture in content control.

    I know that we can rotate the image using user interface but it looks like object model does not provide this functionality to rotate it from code.

    so I suggest you to work with the shape and try to store the rotation in variable.

    then you can try to use that value in future.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 20, 2017 2:48 AM
    Moderator
  • Hi,

    I wanted to inform you about my solution/walkaround for the issue:

    I rotated the image itself and after this I saved this image in my local "C:\Temp" path.

    Further on i created a new PictureContentControl and added the rotated image from the local path.

    var rotatedEmfImage = Image.FromFile(emfImagePath);
    rotatedEmfImage.RotateFlipRotateFlipType.Rotate270FlipNone);
    var rotatedEmfName = $"Image{Guid.NewGuid()}.emf";
    var rotatedEmfPath = Path.GetTempPath() + rotatedEmfName;
     rotatedEmfImage.Save(rotatedEmfPath, ImageFormat.Emf);
     rotatedEmfImage.Dispose();
    
    var picGuid = Guid.NewGuid();
    var pictureContentControl = document.Controls.AddPictureContentControl(nativeControl, $"CHART{picGuid}");
    pictureContentControl.LockContents = false;
    pictureContentControl.Title = chartContentControl.ChartInfo.Title;
    pictureContentControl.Tag = chartContentControl.Key;
    
    var insertedShape = pictureContentControl.Range.InlineShapes.AddPicture(rotatedEmfPath); 
    

    With this approach, I could meet the requirement as far as possible.

    Best regards,

    Philipp

    • Marked as answer by Philipp Fo Tuesday, June 27, 2017 6:46 AM
    Tuesday, June 27, 2017 6:46 AM