We help IT Professionals succeed at work.
Get Started

Trouble grouping shapes in Word VBA

870 Views
Last Modified: 2016-02-11
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?
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE