Trouble grouping shapes in Word VBA

This is driving me crazy.  It seems so easy, but I am obviously missing something.  I have a macro that lets the user select one or more photos, provide a caption, and then it is supposed to groups the photos, put the caption under the group, then group the caption with the grouped photos, so they can all be moved as one unit.

Everything works fine until the very last line of the macro.  It gives me an error saying "grouping is disabled for the selected shapes."  

Sub captionselectedphoto(control As IRibbonControl)

Dim shapesselected As Integer
Dim photos2caption As ShapeRange
Dim phototitle As String

shapesselected = Selection.ShapeRange.count

If shapesselected = 0 Then
    MsgBox "Please select one or more photos to caption, then try again"
    Exit Sub
End If

Set photos2caption = Selection.ShapeRange

If photos2caption.count > 1 Then photos2caption.Group
Application.ScreenRefresh

phototitle = InputBox("Photo title?")

Selection.InsertCaption Label:="Figure", TitleAutoText:="", _
    Title:=": " & phototitle, Position:=wdCaptionPositionBelow, ExcludeLabel:=0

Set captionbox = ActiveDocument.Shapes(ActiveDocument.Shapes.count)

MsgBox photos2caption.Name  'returns "Group 20"
MsgBox captionbox.Name      'returns "Text Box 9"

ActiveDocument.Shapes.Range(Array(photos2caption.Name, captionbox.Name)).Group

End Sub

Open in new window

As you can see, I stuck in a couple msgboxes near the end to tell me the shape names.  The return the expected names.  What is most maddening is that I can run this test macro and it does exactly what I want:
ActiveDocument.Shapes.Range(Array("Group 20", "Text Box 9")).Group

Open in new window

This is what I'm aiming for.  globegroup.jpgIs there something wrong with my syntax?  Can I not put variables in the array statement like this?  What am I doing wrong?
Bryce BassettFreelance VBA programmerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrTribosCommented:
I am looking on my phone - how do you add the caption box?
0
DrTribosCommented:
Oh scratch that I see the test.... try adding doevents before the group
0
DrTribosCommented:
Yikes... weird.  So I made some small changes, namely the addition of...
Debug.Print "Activedocument.Shapes.Range(Array(""" & photos2caption.Name & """, " & """" & captionbox.Name & """)).Group"

Open in new window

after the 2 message boxes (& commented out the message boxes) but before attempting to group.

The code fails, press End.  Go to immediate window and run the debug.print line and it still fails.  

Now, select the caption box so the border is solid and run the debug.print line a 2nd time and the grouping fails again.

Now, instead, select the already grouped images BUT NOT the caption box and the grouping works.  (incidentally it also works if the caption box is selected....)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

DrTribosCommented:
Moral - word is weird.  You need to select photos2caption before you group (line 31)...
Sub captionselectedphoto()

Dim shapesSelected As Integer
Dim photos2caption As ShapeRange
Dim phototitle As String
Dim captionbox As Shape

shapesSelected = Selection.ShapeRange.count

If shapesSelected = 0 Then
    MsgBox "Please select one or more photos to caption, then try again"
    Exit Sub
End If

Set photos2caption = Selection.ShapeRange

If photos2caption.count > 1 Then photos2caption.Group


phototitle = InputBox("Photo title?")

Selection.InsertCaption Label:="Figure", TitleAutoText:="", _
    Title:=": " & phototitle, Position:=wdCaptionPositionBelow, ExcludeLabel:=0

Set captionbox = ActiveDocument.Shapes(ActiveDocument.Shapes.count)

'MsgBox photos2caption.Name  'returns "Group 20"
'MsgBox captionbox.Name      'returns "Text Box 9"

Debug.Print "Activedocument.Shapes.Range(Array(""" & photos2caption.Name & """, " & """" & captionbox.Name & """)).Group"
photos2caption.Select
ActiveDocument.Shapes.Range(Array(photos2caption.Name, captionbox.Name)).Group

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bryce BassettFreelance VBA programmerAuthor Commented:
This works now.  Thank you so much for troubleshooting this!  I thought I had tried to reselect the photo group, but I guess not.
0
DrTribosCommented:
Gotta admit it had me flummoxed for a while too.  Glsd to help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.