Grouping shapes in Word

P

Peter Kinsman

I am trying to group a variable number of shapes to help with the process of
flowcharting a program. I realise that I should really be doing it in
Visio, but I am using Word.
I have based my code on that which is created by a macro -

ActiveDocument.Shapes.Range(Array("AutoShape 4758", "Line 4759", "Line
4760")).Select
Selection.ShapeRange.Group.Select

I have changed this to

ActiveDocument.Shapes.Range(Array(strGroup)).Select
Selection.ShapeRange.Group.Select

Each time a shape or line that is to be added to the group is added to the
Shapes Collection, I add its name to the string variable strGroup. When all
the shapes have been created, if I paste the contents of strGroup into the
brackets it works, but if I use the name of the string, then I get run time
error '9'

The item with the specified name wasn't found.

Has anyone else tried something similar please?

Many thanks

Peter Kinsman
 
P

Peter Hewett

Hi Peter Kinsman

The Array Function expects multiple parameters passed in, one for each element
in the array you want created. So the statement "Array(strGroup)" create a 1
element array, that's value is something like "AutoShape 4758,Line 4759,Line
4760". Which is obviously an invalid shape name.

You need to pass in an array of type variant that contains strings (or integers
if you know the shapes index numbers). This code will do what you want, you
need to separate each shape name with a comma (no spaces other than what are in
the shape name if any):

Public Sub GroupEm()
Dim strGroup As String

strGroup = "Line 8,Oval 5,Rectangle 4"

ActiveDocument.Shapes.Range(VarArray(strGroup)).Select
Selection.ShapeRange.Group.Select
End Sub

Private Function VarArray(ByVal strIn As String) As Variant
Dim lngIndex As Long
Dim astrShape() As String
Dim avarShape() As Variant

astrShape = Split(strIn, ",")
ReDim avarShape(0 To UBound(astrShape))

For lngIndex = 0 To UBound(astrShape)
avarShape(lngIndex) = astrShape(lngIndex)
Next
VarArray = avarShape
End Function

HTH + Cheers - Peter
 
P

Peter Kinsman

Peter

Thank you ever so much for your help.

I tried to reply to you directly, but failed to notice the <nospam> in the
email address, so will repeat my comments to the group.
Am I correct in thinking that you now support the Linux camp?

I think one of my problems was forgetting that Array is a function and not
an array. Because I am still using Office 97, I do not have the Split
function, but I can use the shape names to build up the elements of the
astrShape array instead of concatenating the string strGroup. I must admit
to being somewhat confused over the difference between a string array and a
variant array containing strings, but the important thing is that it works -
after two days of my struggling and some help from someone who actually
knows what they are doing.

This is the latest version of the VarArray function

Private Function VarArray() As Variant
Dim II As Integer
ReDim avarShape(0 To ActiveDocument.Shapes.Count - intFirstShapeNo)
intShapeNo = 0
For II = intFirstShapeNo To ActiveDocument.Shapes.Count
avarShape(intShapeNo) = ActiveDocument.Shapes(II).Name
intShapeNo = intShapeNo + 1
Next
VarArray = avarShape
End Function

intFirstShapeNo is of course set to the number of the first shape in each
group.

Thanks again

Peter
 
P

Peter Hewett

Hi Peter Kinsman

Glad to be able to help.

The reason that it expects a Variant array as opposed to a String array is that
a Variant can hold data of any type. The Range method can accept either a
Variant array of Shape names (string) or a Variant array of shape index numbers
(numeric).

So in light of the above and your code rewrite, you could rewrite it again to
be:

Public Function VarArray(ByVal lngFirstShape As Long) As Variant
Dim lngIndex As Long
Dim avarShape() As Variant

With ActiveDocument.Shapes
ReDim avarShape(0 To .Count - lngFirstShape)
For lngIndex = lngFirstShape To ActiveDocument.Shapes.Count
avarShape(lngIndex - lngFirstShape) = lngIndex
Next
End With
VarArray = avarShape
End Function

and use it:
ActiveDocument.Shapes.Range(VarArray(1)).Select
Selection.ShapeRange.Group.Select

Which will select and group all shapes.

HTH + Cheers - Peter
 
P

Peter Kinsman

Peter

All tried and working - I realise that the line
With ActiveDocument.Shapes
means that the upper limit of the For loop should be just
..Count

Regards

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top