PDA

View Full Version : Solved: Shapes: Some Explanation Please



YellowLabPro
09-30-2007, 04:22 AM
This is for pure learning reasons, no project.
All of the following code is from the help file. I am curious about certain objects and properties that I have not used to become familiar.

In this case wanted to see what .List was about, which took me to Shapes.
I could not get enough information about .List, so I went onto Shapes.

My interest is purely about worksheet shapes, not charts.
Examples 1,2,3, and 4 are where my questions lie.
Examples 5 and 6 were successful.

I have two shapes on my worksheet, drawn from the tools on the Drawing Toolbar.

Ex.2 refers to Shapes(1). I looked for properties on the sheet of the shapes to see how these were referenced. I could not find any where that referes to the properties of these created shapes.
So when it is instructing the shape to be flipped, how is it referencing a particuluar shape.

Ex.3 & 4 refer to the shape by name "Rectangle 1", same basic questions.
How is this shape referenced by this name?


Sub test()
'ex.1
Worksheets(1).Shapes(2).ControlFormat.List = _
Array("cogs", "widgets", "sprockets", "gizmos")
Application.AddCustomList Array("cogs", "sprockets", _
"widgets", "gizmos")
'ex.2
Set myDocument = Worksheets(1)
myDocument.Shapes(1).Flip msoFlipHorizontal
myDocument.Shapes("Rectangle 1").Flip msoFlipHorizontal
'ex.3
Worksheets(1).Shapes(1).Flip msoFlipHorizontal
'ex.4
Worksheets(1).Shapes("Rectangle 1").Flip msoFlipHorizontal
'ex.5
Worksheets(1).Shapes(1).Fill.ForeColor.RGB = RGB(255, 255, 0)
'ex.6
Set myDocument = Worksheets(1)
With myDocument.Shapes.AddShape(msoShapeRightTriangle, _
10, 10, 50, 50).Duplicate
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Flip msoFlipVertical
End With
End Sub


thanks.....

Bob Phillips
09-30-2007, 04:37 AM
Hint.

Shapes are not just charts and items on the drwaing toolbar.

A Forms Listbox is also a shape for example.

YellowLabPro
09-30-2007, 05:02 AM
Ok.... I will pursue that thought.

In the mean time, a few followup questions regarding the example posted plz.

In ex.6- it flips only one of the new shapes, the duplicated triangle- how does the code choose which one of the two shapes apply the instructions, to flip, or color the duplicated shape and not the first shape?

It colors the first shape maroon, and the duplicated triangle yellow. I don't see where the code is getting this information to fill either of them w/ maroon, is code using the last color used? Filling w/ yellow RGB(255, 255,0) is understood, but again as in the first follow-up question here how does it choose- same answer will apply here....

Lastly- depending on your answers to the above questions, this may become evident;
How would I reference the first shape if I wanted to provide instructions specific to this shape rather than the latter?

rory
09-30-2007, 11:33 AM
This line:
myDocument.Shapes.AddShape adds a shape and returns a shape object to the code. You then apply the Duplicate method to this object, which creates a copy of it and returns a reference to that copy. That is the object being used by everything in the With...End With block. I don't see anything about purple in there.

YellowLabPro
09-30-2007, 11:41 AM
Hello Rory,
That was my assumption,


That is the object being used by everything in the With...End With block.

If that is how it handles it, is there a means to identify a Shapes object to manipulate it?

thanks....

Bob Phillips
09-30-2007, 12:08 PM
Only by setting a variable to it whne creating, and using that.

rory
09-30-2007, 12:18 PM
Or if you know its name, or it has some unique identifying characteristic.

YellowLabPro
09-30-2007, 12:36 PM
Rory,
Can you provide an example?

rory
09-30-2007, 02:26 PM
Well, if you know its name, you can just refer to that, as in your example 4. If you know that it is the only dropdown, or of any other type, you can loop through the collection checking the Type and/or AutoShapeType of each shape to find the one you want. You can do the same with TopLeftCell property if you know where your shape is, and so on. Obviously, if you create the shape, it's easiest to just grab a reference to it then.

Bob Phillips
10-01-2007, 01:39 AM
The easiest way has to be to set the variable as you create it as I said earlier, and give it a meaningful name. You then have full control.

YellowLabPro
10-01-2007, 04:55 AM
Thanks Bob and Rory.
Here is wherein the problem resided- (resided, b/c now I get it).
I wanted to flip a shape. (My thought of what Flip is and VBA's was definitely different).
I had a rectangle, its position was vertical. The .flip msoFlipHorizontal was not flipping it horizontally. The shape was not ending up on a different axis.
I thought the code was not identifying the shape, (my confusion started here). So I thought I needed to know some property that would identify the shape. I started looking for properties.

[Bob- I was not using your solution just yet b/c the shape already existed and the question going around in my head was if the shape exists, how do I reference it.]

[Rory, the name "Rectangle 1" does not seem to be doing anything.]

The code in fact was identifying it and was flipping it, but whatever .flip msofliphorizontal means to a rectangle shape I still do not understand, b/c the final appearance does not look any different from whence it began. Maybe it means flip it over, front to back.... I will play w/ this more later.

So how I tested it was to manually select the shape and rotate it slightly. Then running the code, the shape's position was altered.
I also did a simple .Select command, this worked, then it started to fall into place.
I looked for how to rotate it a certain number of degrees and this is what I found:
Using:

Worksheets("Sheet1").Shapes("Rectangle 1").IncrementRotation 90#

Alters the shape's position.

In closing what I was looking to do solve that did not require any solving.
Bob and Rory, your solutions were right on the money- but I could not see it.

Thanks for hanging w/ me on this.

Best regards,

Doug

Bob Phillips
10-01-2007, 06:50 AM
This flips fine for me



Dim shp As Shape
Set shp = ActiveSheet.Shapes.AddShape(msoShapeRightTriangle, 10, 10, 50, 50)
shp.Flip msoFlipVertical

YellowLabPro
10-01-2007, 07:33 AM
Me too, but not rectangles.... can you test it on a rectangle?

Bob Phillips
10-01-2007, 08:18 AM
But a rectangle (or more precisely a square) is the same when flipped.

YellowLabPro
10-01-2007, 09:07 AM
Nope, have to respectfully disagree.
Only in theory, not from practical standpoint, b/c obviously I am incorrect.
My perspective is if a rectangle is instructed to be flipped, it would be be altered to that position, horizontal or vertical.
Anyway- I think I am howling at the moon on this one....
(btw: I wrote this, knowing very likely I am going to get schooled here.... <G>)

rory
10-01-2007, 09:13 AM
Take a playing card. Hold a pencil across its midpoint (either horizontal or vertical) and rotate the card around around the pencil. It produces the same shape, no? :)

YellowLabPro
10-01-2007, 09:28 AM
Rory-
Yep. Before I say anymore, let me mess w/ this some.

Andy Pope
10-02-2007, 01:54 AM
Hi YellowLabPro,

I can see where the possible confusion is coming from. The 2 Flip icons would suggest that the flipping is done on a edge of the shape where as in fact the flipping and rotation is done around the center point of the shape.