none
How to group different shapes using interop.word RRS feed

  • Question

  • I created multiple text boxes and now I need to combine them with a single image. But I can't get their ShapeRange. I have their Shapes object, but I really can't understand how the Shapes.Range() method can be used to return a ShapeRange object with multiple Shapes.

    For example, I now have Shapes allShapes, which has 5 Shapes
    I want to combine the first and third Shapes, how do I get the ShapeRange that only has their two Shapes?
    Wednesday, July 3, 2019 9:43 AM

Answers

  • The sample code is listed in VBA, you can use the following in C#:

    int[] addedShapes = new int[] { 1, 3 };
    Shape tmpShape = document.Shapes.get_Range(addedShapes).Group();
    

    or

    object[] addedShapes = new object[] { "name", "unique2" };
    Shape tmpShape = worksheet.Shapes.get_Range(addedShapes).Group();


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by Jc_914 Friday, July 5, 2019 8:10 AM
    Thursday, July 4, 2019 9:18 AM

All replies

  • Hello Jc,

    VSTO doesn't provide anything for that. I'd suggest posting to an application specific forum or general if you can't find the related one. 

    Anyway, the parameter to the Range property can be an integer that specifies the index number of the shape, a string that specifies the name of the shape, or an array that contains either integers or strings.

    Set myDocument = Worksheets(1) 
    myDocument.Shapes.Range(Array(1, 3)).Fill.Patterned msoPatternHorizontalBrick


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Wednesday, July 3, 2019 8:47 PM
  • Thank you for your answer

    but sorry, I really can't understand what the Array in myDocument.Shapes.Range(Array(1, 3)) is

    I tried to create a System.Array object with an int, but unfortunately it didn't work. I also built an int[] array, and the code failed when it was executed. Can you give me the correct parameters

    I am very grateful for your help.

    Thursday, July 4, 2019 2:17 AM
  • The sample code is listed in VBA, you can use the following in C#:

    int[] addedShapes = new int[] { 1, 3 };
    Shape tmpShape = document.Shapes.get_Range(addedShapes).Group();
    

    or

    object[] addedShapes = new object[] { "name", "unique2" };
    Shape tmpShape = worksheet.Shapes.get_Range(addedShapes).Group();


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by Jc_914 Friday, July 5, 2019 8:10 AM
    Thursday, July 4, 2019 9:18 AM
  • Thank you very much, I successfully solved my problem, your method is very useful
    Friday, July 5, 2019 8:10 AM