none
Ungrouping Shapes causes them to move RRS feed

  • Question

  • I am trying to create some code to retrieve information about shapes in a Word 2003 document, using vb.net 2008. (I have also tried the code in Word VBA, which is what I describe below)

    What I'm looking for is the Top and Left properties of the shapes in the document. When a shape is not part of a group, a simple Shape.Top / Shape.Left gets me the data. When the shape is part of a group, however, those keywords return the Top and Left of the group, not the shapes within.

    This code gives me the same top and left for each shape within the group - the group's top and left!

     

    If sh.Type.ToString = "msoGroup" Then
    For i = 1 To sh.GroupItems.Count
    sh_item = sh.GroupItems.Item(i)
    shapename = sh_item.Name
    'handle shape, including...
    shapetop = sh_item.Top.ToString
    shapeleft = sh_item.Left.ToString
    End If

    So then I figured, simple, just ungroup the shapes and get their locations that way. So, I tried the following code...

    If sh.Type.ToString = "msoGroup" Then
    sh.UnGroup
    sh_Name = sh.Name
    'handle all the shapes, including...
    shapetop = sh.Top.ToString
    shapeleft = sh.Left.ToString
    End If

    And that made my grouped shapes move to the first page (of a 22 page document!) I put some code behind the doc in the vba ide just to play around and try to figure out what was happenning. I tried adding a LockAnchor before the UnGroup - that made all the moving shapes move to the last page of the doc. I put the VBA IDE side by side with the doc and stepped through it, and had randomly scrolled the doc to page 2. Stepping through the code, I watched all the shapes move, one by one, on to page 2!

    I'm exasperated, I have been searching the net with every combination of search terms I can think of. I have learned more about the Word Object Model than I ever wanted to know, even some really esoterically unrelated stuff I can use later, but I have not seen anything remotely like the behavior I am observing.

    Can anyone (a) explain this behavior, and (b) tell me how to get the Top and Left properties of a shape within a group???

    Wednesday, February 2, 2011 3:11 AM

Answers

  • Hi dyfhid,

    The approach below, which seems to be pretty much the same as you're doing, seems to work OK with VBA:

    Sub Demo()
    Dim oShp As Shape, oShpi As Shape, oShpGrp
    With ActiveDocument
      For Each oShp In .Shapes
        If oShp.Type = msoGroup Then
          Set oShpGrp = oShp.Ungroup
          For Each oShpi In oShpGrp
            MsgBox "Top: " & oShpi.Top & vbCr & "Left: " & oShpi.Left
          Next
          '.Undo
          oShpGrp.Regroup
        Else
          MsgBox "Top: " & oShp.Top & vbCr & "Left: " & oShp.Left
        End If
      Next
    End With
    Set oShpGrp = Nothing
    End Sub

    Note the commented-out 'Undo'. If you're worried about ensuring the grouped elements are returned to their original positions after getting the position data, you could use that instead of the 'oShpGrp.Regroup' line.

    Can you tell us more about the locations & anchoring of the grouped items before they were grouped? For example, might they have been anchor-locked to paragraphs that have since been deleted or moved relative to where the group now resides?


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by dyfhid Wednesday, February 2, 2011 4:37 PM
    Wednesday, February 2, 2011 7:01 AM

All replies

  • Hi dyfhid,

    The approach below, which seems to be pretty much the same as you're doing, seems to work OK with VBA:

    Sub Demo()
    Dim oShp As Shape, oShpi As Shape, oShpGrp
    With ActiveDocument
      For Each oShp In .Shapes
        If oShp.Type = msoGroup Then
          Set oShpGrp = oShp.Ungroup
          For Each oShpi In oShpGrp
            MsgBox "Top: " & oShpi.Top & vbCr & "Left: " & oShpi.Left
          Next
          '.Undo
          oShpGrp.Regroup
        Else
          MsgBox "Top: " & oShp.Top & vbCr & "Left: " & oShp.Left
        End If
      Next
    End With
    Set oShpGrp = Nothing
    End Sub

    Note the commented-out 'Undo'. If you're worried about ensuring the grouped elements are returned to their original positions after getting the position data, you could use that instead of the 'oShpGrp.Regroup' line.

    Can you tell us more about the locations & anchoring of the grouped items before they were grouped? For example, might they have been anchor-locked to paragraphs that have since been deleted or moved relative to where the group now resides?


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by dyfhid Wednesday, February 2, 2011 4:37 PM
    Wednesday, February 2, 2011 7:01 AM
  • Paul,

    Thank you SO much!!!

    It was the Undo that did it. Using the ReGroup, the shapes stayed on the first page after the routine was done. Using the Undo put them back where they belonged.

    It was interesting watching it run. The doc has to be made visible to enable pagination. I had the VBA IDE on one monitor and the doc opened on another, and you could watch the grouped shapes appear (in their respective correct locations) on page one, then disappear when the .Undo was encountered :)

    The Top and Left properties were listed correctly for each and every shape in the document, and now I can let go my exasperation and move on with this project, thanks to you. Your name is now in the comments of my code, credited with solving the problem!

    Oh, and regarding the locations & anchoring of the grouped items before they were grouped, unfortunately I know nothing about it. The documents I am deconstructing are work instructions created in another department. It's entirely possible that your guess is correct, that anchors reside where deleted paragraphs used to be, but I couldn't say for sure, I am sorry.

    Again, thank you so much for your help! This thing had me stuck for nearly two days, making me have even less hair than I started out with, which wasn't much!

    David Taylor
    ASQ CQT, MCITP DBA, SQL Server 2008
    PASS Application Development Virtual Chapter Volunteer Coordinator

    Wednesday, February 2, 2011 4:46 PM