none
Word VBA finding shapes

    Question

  • In Word 2010  VBA I want to advance to the next shape in a document and select the shape. The Next method of the Browser object seems ideal as it finds the next shape whether it is floating or inline. The following code moves the cursor to the anchor point of the next shape but does not select the shape:

            Application.Browser.Target = wdBrowseGraphic

        Application.Browser.Next

    The only way that I can see to then select the shape is to extend the range by one character and select the first shape in the range:

           Set aRange = Selection.Range

               With aRange

                    .MoveEnd unit:=wdCharacter

                   If .ShapeRange.Count > 0 Then  .ShapeRange(1).Select

                   If .InlineShapes.Count > 0 Then  .InlineShapes(1).Select

           End With

    The problem with this approach is that there may be more than one shape anchored at the same point and it seems impossible to determine which of the shapes Browser has found.

     Is there a way to do this?

    Sunday, September 22, 2013 4:58 AM

Answers

  • Hi Kroak888,

    As you might be aware, you have to work with Shapes and Inline Shapes separately. The following sample code will locate both inline and floating style shapes and select them one at a time for further processing.

    If you need more help, let us know what you are trying to accomplish with the various shapes after you have selected them.

    Sub FindShapes()
        Dim doc As Word.Document, rng As Word.Range
        Dim shp As Word.Shape, iShp As Word.InlineShape
        
        Set doc = ActiveDocument
        For Each shp In doc.Shapes
            shp.Select
            Debug.Print shp.Type
        Next
        For Each iShp In doc.InlineShapes
            iShp.Select
            Debug.Print iShp.Type
        Next
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, September 24, 2013 11:23 AM
  • I have worked out a convoluted way to find the next shape even if that shape is in a cluster of shapes having the same anchor point.

    The following code will determine a shapes position in the cluster. It is then a matter of advancing to the next shape in the cluster or if at the last shape, moving to the next shape after the cluster.

    ' determine the number of shapes remaining in its cluster
    ' a shape must be currently selected  
    Set aRange  =  Selection.Range
    aRange.MoveEnd Unit:=wdCharacter
    m = aRange.ShapeRange.Count
    ' move forward and back to put the cursor at the first tag
    With Application
      .Browser.Target = wdBrowseGraphic
      .Browser.Next
      .Browser.Previous
    End With
    Set aRange  =  Selection.Range
    aRange.MoveEnd Unit:=wdCharacter
    n = aRange.ShapeRange.Count
    ClusterPosition = n – m + 1
    

    I was able to write a routine to advance from wherever the cursor is in the document to the next floating or inline shape. Including the ability to go forward or backwards, I ended up with 75 lines of code.

    Surely there is a simpler way.

    Friday, September 27, 2013 3:17 AM

All replies

  • Hi,

    Do you want to select all the shapes in a same shape anchor or a shape in a shape anchor?

    Here is a sample to continually select all the shapes in a document for your reference.

    If ActiveDocument.shapes.Count > 0 Then
      For Each Ssh In ActiveDocument.shapes
        Ssh.Select
      Next Ssh
    End If
    Tuesday, September 24, 2013 9:56 AM
  • I want to select the next (or previous) shape or inlineshape from wherever the cursor currently is. The Browse object which can be run from the browse arrows at the bottom of the vertical scroll bar does this as does the VBA code Application.Browser.Next or Application.Browser.Previous.

    However, Browse moves the cursor to the anchor point of the next shape but does not select the shape itself.

    When using the browse arrows it is not always obvious which shape browse has found as the shape can be offset from the anchor point. With the VBA code it is easy to select the appropriate shape provided there is only one shape anchored at that anchor point. But there is a problem if there is more than one shape anchored at the point.

    What I am seeking is a solution to determining in VBA what shape to select when there are multiple shapes anchored to the one anchor point.

    It seems that Word places invisible anchor tags (one for each shape) at the paragraph mark and it does not seem possible to access these in VBA.


    Tuesday, September 24, 2013 11:11 AM
  • Hi Kroak888,

    As you might be aware, you have to work with Shapes and Inline Shapes separately. The following sample code will locate both inline and floating style shapes and select them one at a time for further processing.

    If you need more help, let us know what you are trying to accomplish with the various shapes after you have selected them.

    Sub FindShapes()
        Dim doc As Word.Document, rng As Word.Range
        Dim shp As Word.Shape, iShp As Word.InlineShape
        
        Set doc = ActiveDocument
        For Each shp In doc.Shapes
            shp.Select
            Debug.Print shp.Type
        Next
        For Each iShp In doc.InlineShapes
            iShp.Select
            Debug.Print iShp.Type
        Next
    End Sub
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, September 24, 2013 11:23 AM
  • Hi Rich

    I want to advance to and select the next shape in the document whether it is floating or inline. Having selected the shape I can then decide if I want to do anything with it such as move, re-size or rotate before moving on to the next shape.

    The problem with "For Each shp in ActiveDocument.shapes" and its equivalent for inline shapes is that they find shapes in the order they were inserted, not the order that they appear in the document.

    Application.Browser does what I want except where there are multiple shapes anchored at the same point. Inlineshapes are not an issue as they do not have an anchor and they act like a character.

    From what I can surmise, an anchor is an invisible tag that is placed just before the paragraph mark. If there are several shapes anchored to the one paragraph then there are several individual tags The code Application.browser.next moves the cursor to just before the paragraph mark, I assume that successive tags are selected although the visible cursor does not move until after the last shape in the shape cluster. You can see this effect by using the browse arrows at the bottom of the vertical scroll bar on a document that has a cluster of shapes.

    There does not seem to be a way in VBA to determine which tag of a multiple tag set is selected by Application.Browser and therefore it is not possible to determine the appropriate shape in the cluster.

    • Edited by Kroak888 Wednesday, September 25, 2013 10:13 PM
    Wednesday, September 25, 2013 10:04 PM
  • I have worked out a convoluted way to find the next shape even if that shape is in a cluster of shapes having the same anchor point.

    The following code will determine a shapes position in the cluster. It is then a matter of advancing to the next shape in the cluster or if at the last shape, moving to the next shape after the cluster.

    ' determine the number of shapes remaining in its cluster
    ' a shape must be currently selected  
    Set aRange  =  Selection.Range
    aRange.MoveEnd Unit:=wdCharacter
    m = aRange.ShapeRange.Count
    ' move forward and back to put the cursor at the first tag
    With Application
      .Browser.Target = wdBrowseGraphic
      .Browser.Next
      .Browser.Previous
    End With
    Set aRange  =  Selection.Range
    aRange.MoveEnd Unit:=wdCharacter
    n = aRange.ShapeRange.Count
    ClusterPosition = n – m + 1
    

    I was able to write a routine to advance from wherever the cursor is in the document to the next floating or inline shape. Including the ability to go forward or backwards, I ended up with 75 lines of code.

    Surely there is a simpler way.

    Friday, September 27, 2013 3:17 AM