none
How to select a shape in C#? RRS feed

  • Question

  • I have recorded this macro in Word 2016 for a shape selection

      Selection.HeaderFooter.Shapes.Range(Array("Text Box 2")).Select

    Can anyone please help to convert this VBA line to c# equivalent?

    I assume it should be something like

    this.Application.Selection.HeaderFooter.Shapes.Range( Array("Text Box 2")).Select

    where this is a reference to a Word document in my code.

    I understand that Array keyword should be replaced with something else. But, I don't know what it is. I tried object["Text Box 2"] instead of Array("Text Box 2") and this did not work for me (error - 'object' is a type, which is not valid in the given context).

    Thanks

    Sunday, March 12, 2017 11:35 PM

Answers

  • Hello,

    We are glad that you have figured it out and thanks for sharing. I suggest you mark your solution as answer, and it would help others who have similar issue.

    Thanks for your understanding.

    Regards,

    Celeste


    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.

    • Marked as answer by SZBM Monday, March 27, 2017 11:42 PM
    Thursday, March 16, 2017 2:26 AM
    Moderator

All replies

  • Hello,

    Please try to use

    this.Application.Selection.HeaderFooter.Shapes["Text Box 2"].Select();

    Regards,

    Celeste


    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.

    • Proposed as answer by Jon Skaranger Wednesday, March 15, 2017 4:44 PM
    Monday, March 13, 2017 5:00 AM
    Moderator
  • Hi Celeste

    Thank you for you response. I tried Shapes["Text Box 2"] selection. This threw an exception "The item with the specified name wasn't found". My text box contains a picture "Picture  2" and also some text . So, I tried to put the picture name  instead of "Text Box 2" that is Shapes["Picture  2"] and it was selected OK. However, I need to select the whole textbox i.e. picture and text all together. Any ideas?

    Thanks

    SZV







    • Edited by SZBM Thursday, March 16, 2017 1:37 AM
    Wednesday, March 15, 2017 9:49 PM
  • OK. Have figured this out myself. This works for me

    object[] array = new object[] { "Text Box 4" };
      this.ActiveWindow.Selection.HeaderFooter.Shapes.Range(array).Select();
    Wednesday, March 15, 2017 10:52 PM
  • Hello,

    We are glad that you have figured it out and thanks for sharing. I suggest you mark your solution as answer, and it would help others who have similar issue.

    Thanks for your understanding.

    Regards,

    Celeste


    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.

    • Marked as answer by SZBM Monday, March 27, 2017 11:42 PM
    Thursday, March 16, 2017 2:26 AM
    Moderator