none
Grouping Shape as a Shape in Interop.Word 12.0 using C# RRS feed

  • Question

  • Hi All. I'm newbie in Word Development using Interop.Word 12.0. I've add some shape into Word Document. This is my current code (add some shape into Word Shape Collection):

    Document d = new Document();

    object missing = Type.Missing;

    Shape box_1 = d.Shapes.AddShape((int)MsoAutoShapeType.msoShapeRectangle, 0, 0, 100, 100, ref missing);

    Shape box_2 = d.Shapes.AddShape((int)MsoAutoShapeType.msoShapeRectangle, 0, 0, 100, 20, ref missing);

    Can I group that shapes using Group feature of shape in word document in C#? Thank all.

    Saturday, July 16, 2011 11:19 PM

Answers

  • Hi Lufti

    Word 2007 (12) lives in a sort of "nether-nether world" when it comes to the graphics interface. Office 2007 changed the graphics engine from that used in 2003 and earlier (back to 97, as I recall). PowerPoint implemented the change fully; Word only partially. So in Word, some kinds of Shapes belong to the old graphics engine, while others to the new AND the UI and VBA don't share the same graphics capability for all things. (I researched this in some detail a few years ago, but no longer have all the details about what I discovered.) This just as background material...

    Grouping in Word 2007 is "sometimes you can, sometimes you can't". You can when the Shapes are all managed by the same graphics engine; you can't when they're managed using different graphics engines.

    In your case, they come from the same graphics engine because they're all AutoShapes. So, yes, you can group them. The basic syntax is:

      Range..ShapeRange.Group()

    To group all the Shapes in a document: theDoc.Content.ShapeRange.Group();

    BUT a remark to the code snippet you show us: The Office object models do not support the use of the keyword "new" for anything except the Application object. (There are some exceptions in the case of new object models, but not for the core apps.) These classes are sealed and the object models provide an ADD method when you want to add an object of some kind. So you need to change that first line of code to: Document d = wdApp.Documents.Add(//params here);

     


    Cindy Meister, VSTO/Word MVP
    Sunday, July 17, 2011 6:54 AM
    Moderator

All replies

  • Hi Lufti

    Word 2007 (12) lives in a sort of "nether-nether world" when it comes to the graphics interface. Office 2007 changed the graphics engine from that used in 2003 and earlier (back to 97, as I recall). PowerPoint implemented the change fully; Word only partially. So in Word, some kinds of Shapes belong to the old graphics engine, while others to the new AND the UI and VBA don't share the same graphics capability for all things. (I researched this in some detail a few years ago, but no longer have all the details about what I discovered.) This just as background material...

    Grouping in Word 2007 is "sometimes you can, sometimes you can't". You can when the Shapes are all managed by the same graphics engine; you can't when they're managed using different graphics engines.

    In your case, they come from the same graphics engine because they're all AutoShapes. So, yes, you can group them. The basic syntax is:

      Range..ShapeRange.Group()

    To group all the Shapes in a document: theDoc.Content.ShapeRange.Group();

    BUT a remark to the code snippet you show us: The Office object models do not support the use of the keyword "new" for anything except the Application object. (There are some exceptions in the case of new object models, but not for the core apps.) These classes are sealed and the object models provide an ADD method when you want to add an object of some kind. So you need to change that first line of code to: Document d = wdApp.Documents.Add(//params here);

     


    Cindy Meister, VSTO/Word MVP
    Sunday, July 17, 2011 6:54 AM
    Moderator
  • Hi Cindy, I've try your suggestion using Group/Ungroup method. And that's work. Thanks for your advice. Are there anyway to disable the Ungroup feature only on my group Shape using C# and Interop.Word 12.0 (Word 2007)? Thanks again.
    Tuesday, August 23, 2011 2:03 AM