Create array based on shape name or tag

L

Lisa

Essentially, I'm trying to:
· Check all the shapes on a given slide to determine if they meet certain
criteria (shape's name contains a specific string, or shape's tag has a
specific value)
· If a shape meets the criteria, add it to an array
· Use the array to group the shapes or to apply properties (fill, line, etc)
to the shapes in the array

For example, a condensed version of my current code looks something like this:
myArray = Array("Icon_Hat", "Icon_Tree", "Icon_Star")
With myShapes.Range(myArray).Group
.Name = "myTestEffort"
With .Fill
.Solid
.ForeColor.RGB = RGB(255, 153, 51)
End With
.Line.Visible = msoFalse
End With

Instead of defining the array by listing each shape by name, I'd like to be
able to say something like
If myShape.Name Like "Icon_*" Then ... (put it in myArray)
OR
If myShape.Tags("ImgStyle") = "Icon" Then ... (put it in myArray)

Unfortunately, I keep getting stuck at the "put it in myArray" part.

If someone can tell me how to do this -- or refer me to appropriate
documentation, I'd really appreciate it.

Thanks in advance!
 
D

David M. Marcovitz

I could be wrong, but I don't think that PowerPoint can do the Like thing
that you want. You could write more complex If statements, using such
string manipulation functions as Left.
--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
L

Lisa

Thanks for the response, David...
Fortunately, the Like thing does seem to work in PPT... For example see below.

My problem is figuring out how to put the items that meet the Like criterion
into an array so I can group them... Or group them somehow (I'm not married
to the "array" notion, it's just my best guess as to how I could "collect" a
bunch of stuff that meets the criteria so I can use .group).

EXAMPLE: I have sixteen shapes on a slide named "TestArea." One is named
"Olive," another "Olboy," and a third named "Ollaly." The other shapes have
names that don't begin with "Ol." This macro applies a red fill to the three
"Ol*" and leaves the others alone.)

Option Explicit
Dim myShapes As Shapes
Dim myShape As Shape

Sub ThisOlTest()
Set myShapes = ActivePresentation.Slides("TestArea").Shapes
For Each myShape In myShapes
If myShape.Name Like "Ol*" Then
With myShape.Fill
.Visible = msoTrue
.Solid
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0#
End With
End If
Next
End Sub
 
D

David M. Marcovitz

Sorry, I must have misread your post. I'm glad that Like works for you.
I've never used it in PowerPoint, so I learned something new.

Unfortunately, I've never done quite what your doing (grouping from an
array) so I'm not sure how it will work, but you seem to know that part.
I'm not sure if you will have to Dim the array to the right size. If you
know how many items meet your criteria, you could Dim it in advance. If
you don't, you could run through your loop twice. The first time, you
could count how many items. Then, use Redim to set the size of your
array. Then loop through again and fill your array. This is assuming that
you actually need to know the size of the array, and I'm not sure you do.

If you are working in Normal View, you could select each object and group
the selection instead of trying to build an array.

Good luck. I wish I had a few minutes to play with this and give you more
specific advice, but I don't right now.

--David

--
David M. Marcovitz
Microsoft PowerPoint MVP
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
 
L

Lisa

Thanks again, David... Grouping the shapes in Normal view isn't an option
under the circumstances (a macro to build a slide with specific elements in
case somebody deletes or tinkers with the slide -- after the file is outside
my hot little hands ;-)

I'm brand new to arrays (and pretty green with VBA in general), but via Help
files, am able to group shapes using an array. For example, see below.

My big hurdle is adding the shapes to the array programatically, rather than
by typing out the names. Have tried a couple dozen approaches -- all of which
will list the shape names in message boxes, or whatever, in a way that looks
like it might work, -- and all of which give me a runtime error when I try to
use the list to define the content of the array. SIGH

Anyhoooo, I appreciate your input and tips re. arrays in general.

EXAMPLE GROUPING ITEMS IN AN ARRAY
The following sub is similar to previous example (applies red fill to three
named shapes) -- after grouping the shapes.

Sub AnotherTest()
Dim myShapes As Shapes
Dim myShape As Shape
Dim TestArray As Variant
Set myShapes = ActivePresentation.Slides("TestArea").Shapes
TestArray = Array("Olive", "Ollaly", "OlBoy")

With myShapes.Range(TestArray).Group
.Name = "Testing"
With .Fill
.Visible = msoTrue
.Solid
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0#
End With
End With
End Sub
 
L

Lisa

Yeehaw, Yippee, and THANK YOU.
That's exactly what I needed. The example below applies your code to the
test/example I gave earlier. And it works perfectly.
Can't tell you how much I appreciate your help!!!
(And after a nap, I'll see if I can understand how/why your code works and
none of my attempts did... Part, but not all, of my problems were due to
missing the "extra monkey motion" with the array ;-)

UPDATED EXAMPLE
Option Explicit
Dim myShapes As Shapes
Dim myShape As Shape
Dim rayShapes() As String

Sub ThisOlTest2()
'Group and fill shapes named "Ol*" and fill shapes named "Ico*"

Set myShapes = ActivePresentation.Slides("TestArea").Shapes

' Group and apply red fill to all shapes with names beginning with "Ol"
ReDim rayShapes(1 To 1) As String
For Each myShape In myShapes
If myShape.Name Like "Ol*" Then
rayShapes(UBound(rayShapes)) = myShape.Name
ReDim Preserve rayShapes(1 To UBound(rayShapes) + 1) As String
End If
Next
With myShapes.Range(rayShapes).Group
.Name = "ThankYouSteve"
With .Fill
.Visible = msoTrue
.Solid
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0#
End With
End With
' Apply green fill to all shapes with with names beginning with "Ico" _
(don't group the shapes; just apply fill)
ReDim rayShapes(1 To 1) As String
For Each myShape In myShapes
If myShape.Name Like "Ico*" Then
rayShapes(UBound(rayShapes)) = myShape.Name
ReDim Preserve rayShapes(1 To UBound(rayShapes) + 1) As String
End If
Next
myShapes.Range(rayShapes).Fill.ForeColor.RGB = RGB(0, 255, 0)

End Sub
 
L

Lisa

Agree completely re. the Help files on arrays. Heck, I have trouble with the
Help files in general -- though I have to admit: the more I learn about VBA
the better able I am to glean something useful from VBA Help... Don't think
I'd have ever gotten this array thing down without your help, though.
 

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