SELECT CASE for identical objects

Zauberkind
2StarLounger
Posts: 141
Joined: 21 Oct 2011, 10:08

SELECT CASE for identical objects

Post by Zauberkind »

Greetings,
I am building a state machine whose actions and state transitions depend on a single input (click), and which object the input came from. In this case, the objects are all PowerPoint shapes.
Using a Christmas-tree of IF-THEN-ELSEIF... statements is ugly and hard to read.
Life would be much simpler if I could write:

Code: Select all

Function EventHandler
SELECT CASE Object
CASE IS Object1 ' using the classic VBA <object1> IS <object2>
...
CASE IS Object2
...
CASE ELSE
END SELECT
Life is never that simple. :groan:

Also possible, but misleading and inelegant is:

Code: Select all

Function EventHandler(Object)
SELECT CASE TRUE ' TRUE? Why TRUE? where did that come from? you ask.
                 ' Dunno, but zero causes weird results!
CASE Object IS Object1, Object IS Object2
...
CASE Object IS Object3
...
CASE ELSE
END SELECT
Q: Is there an elegant way to do this? in VBA?

TIA for any insights.

Regards
Zk
Last edited by Zauberkind on 31 Mar 2014, 13:15, edited 1 time in total.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: SELECT CASE for identical objects

Post by Jan Karel Pieterse »

Perhaps TypeName(Object) helps?
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT CASE for identical objects

Post by HansV »

I don't think the second code fragment will do what you want - when I tried it, all shapes were considered to be equal.

If you can ensure that each shape has a unique name, you could use

Select Case object.Name
Best wishes,
Hans

Zauberkind
2StarLounger
Posts: 141
Joined: 21 Oct 2011, 10:08

Re: SELECT CASE for identical objects

Post by Zauberkind »

Greetings,

Thanks for the suggestions.

Unfortunately, TypeName(Object) is always msoAutoShape, so that doesn't go far.

The uniqueness of Object Names is enforced by the object model, so that could work. It requires a bit more effort for maintenance, but ...

I find the statement "when I tried it, all shapes were considered to be equal" somewhat disturbing. It conflicts with my observations in PPt 2003. I've hung an example for you to play with. I'd be interested in your results.

Regards,
Zk
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT CASE for identical objects

Post by HansV »

Shamefacedly, I have to admit that I tested in Word, mistakenly thinking that the MSOffice shapes behave the same in all Office applications. I was wrong. They behave differently in Word than in PowerPoint.

By the way, it *is* possible to create shapes with the same name:

The result of running

Code: Select all

Sub NameShapes()
    Dim shp As Shape
    For Each shp In ActiveWindow.View.Slide.Shapes
        shp.Name = "Zauberkind"
    Next shp
    For Each shp In ActiveWindow.View.Slide.Shapes
        Debug.Print shp.Name
    Next shp
End Sub
is

Zauberkind
Zauberkind
Zauberkind
Zauberkind
Best wishes,
Hans

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: SELECT CASE for identical objects

Post by Jan Karel Pieterse »

And Object.AutoShapeType?
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

Zauberkind
2StarLounger
Posts: 141
Joined: 21 Oct 2011, 10:08

Re: SELECT CASE for identical objects

Post by Zauberkind »

Greetings HansV,

I can hear the theme from Twilight Zone playing in the back of my head...

I copied your code into my presentation and ran it.

The first object was renamed successfully.
The second object threw an exception: "Error 70: Permission Denied"
Is PPT 2003 that different? or was that Word again?

Regards,
Zk

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT CASE for identical objects

Post by HansV »

This time I tested in PowerPoint 2010. It doesn't matter whether the presentation is a PowerPoint 97-2003 presentation (.ppt) or a PowerPoint 2007-2010 presentation (.pptm).
S269.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

Zauberkind
2StarLounger
Posts: 141
Joined: 21 Oct 2011, 10:08

Re: SELECT CASE for identical objects

Post by Zauberkind »

OK, I'll trade you...
Verweigert.jpg
Regards,
Zk :scratch:
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT CASE for identical objects

Post by HansV »

German is stricter than English, I guess... :grin:
But seriously, I have no idea what causes the difference. Sorry!
Best wishes,
Hans

Zauberkind
2StarLounger
Posts: 141
Joined: 21 Oct 2011, 10:08

Re: SELECT CASE for identical objects

Post by Zauberkind »

Hmmm...
I admit to puzzlement, too!
Puzzled about the difference - that's odd; more puzzled about the namespace issue - that seems counter-intuitive.
If I can access .Shapes("ShapeName") using "ShapeName" as an identifier, what do I get if more than one shape has the same name? Or has that indexing method been overtaken by "progress"?
Regards (and :thankyou:)
Zk

User avatar
HansV
Administrator
Posts: 78386
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SELECT CASE for identical objects

Post by HansV »

Zauberkind wrote:If I can access .Shapes("ShapeName") using "ShapeName" as an identifier, what do I get if more than one shape has the same name?
The first one with that name in the order of creation, as far as I can tell.
Best wishes,
Hans