vba_sample_shapes

advertisement
Loop Through Shapes
Add Some Shapes
This is Title 1
Playing With Shapes
DeleteOtherShapes
This is Subtitle 1
Run the Slideshow to click the Button
This is a textbox. It will not be deleted either. Click Add Some
Shapes, then click DeleteOtherShapes.
Sample Code
Sub pptPractice()
Dim myDocument As Slide
Dim myNewRectangle As Shape
Dim myRectangle As Shape
Set myNewRectangle = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 50, 50, 100, 100)
myNewRectangle.Name = "SimpleRectangle"
Set myRectangle = ActivePresentation.Slides(1).Shapes.Item(4)
Set myDocument = ActivePresentation.Slides(1)
With myDocument.Shapes
gradStyle1 = .Item(myRectangle.Name).Fill.GradientStyle
With .AddShape(msoShapeRectangle, 0, 0, 40, 80).Fill
.ForeColor.RGB = RGB(128, 0, 0)
.OneColorGradient msoGradientDiagonalUp, 1, 1
End With
End With
End Sub
Display Shape Name and GradientStyle
Sub LoopThroughShapes()
Dim shp As Shape
For Each shp In
ActivePresentation.Slides(1).Shapes
MsgBox "Shape: " & shp.Name & vbCrLf &
"GradientStyle: " & shp.Fill.GradientStyle
MsgBox “The Type is: “ &shp.Type
Next
GradientStyle is a read-only
End Sub
property, but once you know its
value you can set
OneColorGradient as seen in the
previous example.
Shapes have integer values, but also constants.
Sub DeleteShapesExceptButtonsAndTitles()
' Deletes all the Shapes except Buttons and Titles
Dim shp As Shape
' Values for Type
'shp.Type: 1 = Rectangle, 14 = Titles? , 12 = CommandButtons, 17 = Textbox
For Each shp In ActivePresentation.Slides(1).Shapes
Select Case shp.Type
Case msoShapeRectangle ' or value of 1
MsgBox "I'm deleting a rectangle"
shp.Delete
Case 14
MsgBox "I won't delete the title."
Case 12
MsgBox "I won't delete this one.", vbOKOnly, "I found a CommandButton"
End Select
Next
End Sub
References
•
•
Working with AutoShapeTypes (such as msoShapeRectangle):
http://msdn.microsoft.com/en-us/library/aa212631(office.11).aspx
AddShape method for CanvasShapes and Shapes Collection:
http://msdn.microsoft.com/en-us/library/aa171541(office.11).aspx
Download