none
How to identify a selected shape on a drawing canvas in VBA RRS feed

  • Question

  • Here is the case.
    A document contains a drawing canvas with several shapes. User selects one of the shapes by clicking on it.
    Now I want my VBA code to say define a hyperlink on this shape. In order to do so, I have to be able to identify the selected shape on the canvas. The Selection object returns the canvas itself, I cannot find a way to identify which of the canvasitems that is selected by the user. 

     

    Friday, May 13, 2011 2:12 PM

Answers

  • If a shape on the canvas is selected, the Selection should be the Canvas, and it should have "HasChildShapeRange" set to True, and the ChildShapeRange will be the selected shape.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Friday, May 13, 2011 4:37 PM

All replies

  • If a shape on the canvas is selected, the Selection should be the Canvas, and it should have "HasChildShapeRange" set to True, and the ChildShapeRange will be the selected shape.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Friday, May 13, 2011 4:37 PM
  • Thanks for your answer, which certainly led me in the right direction. However, the World of Word is not as straight-forward as one might wish. Here's what I have discovered:

    In Word 2007:
    The Drawing Canvas is always selection.type 8 (wdSelectionShape) whether it is inline with the text or not.
    When a shape on the canvas is selected, HasChildShapeRange is True and ChildShapeRange is the selected shape, like your reply stated.
    However, when trying to insert a hyperlink on this shape, the hyperlink always ends up on the canvas itself.
    Activedocument.hyperlinks.add Selection.ChildShapeRange(1), <address>
    A selected shape outside the canvas returns HasChildShapeRange=False and ChildShapeRange.Count=0, as expected.

    In Word 2010:
    The Drawing Canvas returns Selection.Type=7 (wdSelectionInlineShape) Or 8 (wdSelectionShape) consistently.
    If the document is in Compatibility Mode (or .doc), Word 2010 behaves like Word 2007, in that the hyperlink is placed on the canvas itself.
    A native docx document draws the canvas differently in Word 2010, with a thicker frame with soft rounded edges.
    When a shape (or several) on this canvas is selected, the Selection object wrongly returns HasChildShapeRange=False, but ChildShapeRange.Count returns correct number of shapes selected. And, the hyperlink is correctly connected to the selected shape on the canvas. Yess!!
    I also noted that a single shape selected outside the canvas returns ChildShapeRange.Count=1, with ChildShapeRange(1) returning the shape itself.

    Bottom line, I am able to get the results I wanted, in Word 2010, and by checking on ChildShapeRange.Count instead of HasChildShapeRange.

    Svein


    Monday, May 16, 2011 9:46 AM
  • Changes to the drawing engine in Word were partially implemented in 2007, and fully so in 2010. The results of your investigation are certainly believable, but it sounds like 2010, at least, is good for you.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, May 16, 2011 10:37 AM