none
Why position of the shapes are modified while ungrouping? RRS feed

  • Question

  • Hi Everyone,

    I am using below code for UnGrouping the Group shapes.

     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document document = word.Documents.Open("FileName.doc");

                foreach (Shape shape in document.Shapes)
                {
                    try
                    {
                        if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoGroup)
                        {
                            ShapeRange range = shape.Ungroup();
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }

                document.SaveAs("Result.docx",WdSaveFormat.wdFormatDocumentDefault); 

    While saving this document, the position of the shapes are changed. Shape was hidden by another floating items. Why this happend, while checking with simple document this code working fine. Can anyone help me for this?


    Tuesday, August 16, 2016 6:43 AM

Answers

  • To do something like that, you need to store the group's .TopRelative, .LeftRelative, .RelativeVerticalPosition & .RelativeHorizontalPosition properties, plus the .ParentGroup.Top and .ParentGroup.Left values of each of the grouped shapes, then ungroup them, after which you could reposition each shape using the group's properties and that shape's stored position. It would be far better to resolve all the issues I referred to in my previous post before grouping; that way none of this circumlocution should be required.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, August 18, 2016 6:34 AM

All replies

  • >>While saving this document, the position of the shapes are changed. Shape was hidden by another floating items.

     

    How do you group the shapes in this document, manually or at runtime? Is there any multi combination for these shapes? What kind of wrapping style for these shapes? What is "another floating item" and how it is added? Is it set to Always in the front?

    To avoid being hidden, you could use Shape.ZOrder method to bring it to front.

     shape.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoBringToFront);


     

    Wednesday, August 17, 2016 5:39 AM
    Moderator
  • I have created using Word application and this group shape have InFrontOfText wrapping style. Floating item vertical position is relative to paragraph.

    I suspect the paragraph also moved in previous page.

    Setting Zorder index was not proper solution here. I need to fit the shape at its own position

    Wednesday, August 17, 2016 1:05 PM
  • The most likely explanations for objects moving when ungrouped are:
    1. They were originally anchored to different locations that are no longer in the same relative positions on the page;
    2. Their horizontal and/or vertical relative/absolute positions were not originally all defined the same way; and/or
    3. They originally had different 'move with text' and/or 'allow overlap' settings.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, August 18, 2016 2:07 AM
  • Hi Paul,

    Thanks for your reply.

    Is it possible to keep all shapes without changing its position?

    Regards,

    Vijay

    Thursday, August 18, 2016 3:53 AM
  • To do something like that, you need to store the group's .TopRelative, .LeftRelative, .RelativeVerticalPosition & .RelativeHorizontalPosition properties, plus the .ParentGroup.Top and .ParentGroup.Left values of each of the grouped shapes, then ungroup them, after which you could reposition each shape using the group's properties and that shape's stored position. It would be far better to resolve all the issues I referred to in my previous post before grouping; that way none of this circumlocution should be required.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, August 18, 2016 6:34 AM