目次
Background
I am currently working with PowerPoint VBA. I wanted to create a feature that “displays a message box listing the name and type of every shape on a slide.”
Here is the solution I implemented.
The Code
Below is the VBA code. This script retrieves all shapes from the first slide and lists their names and types in a message box.
Private Sub CommandButton_run_Click()
' Get Slide 1
Dim pptSlide As Slide
Set pptSlide = ActivePresentation.Slides(1)
' Create the list of shapes
Dim shapeList As String
shapeList = "List of shapes on Slide 1:" & vbCrLf
Dim pptShape As Shape
For Each pptShape In pptSlide.Shapes
shapeList = shapeList & "Name: " & pptShape.Name & ", Type: " & ShapeTypeName(pptShape.Type) & vbCrLf
Next pptShape
' Display in a message box
MsgBox shapeList, vbInformation
End Sub
' Function to convert the shape type ID to a readable string
Function ShapeTypeName(shapeType As MsoShapeType) As String
Select Case shapeType
Case msoAutoShape
ShapeTypeName = "AutoShape"
Case msoCallout
ShapeTypeName = "Callout"
Case msoCanvas
ShapeTypeName = "Canvas"
Case msoChart
ShapeTypeName = "Chart"
Case msoComment
ShapeTypeName = "Comment"
Case msoDiagram
ShapeTypeName = "Diagram"
Case msoEmbeddedOLEObject
ShapeTypeName = "Embedded OLE Object"
Case msoFormControl
ShapeTypeName = "Form Control"
Case msoFreeform
ShapeTypeName = "Freeform"
Case msoGroup
ShapeTypeName = "Group"
Case msoIgxGraphic
ShapeTypeName = "SmartArt"
Case msoInk
ShapeTypeName = "Ink"
Case msoInkComment
ShapeTypeName = "Ink Comment"
Case msoLine
ShapeTypeName = "Line"
Case msoLinkedOLEObject
ShapeTypeName = "Linked OLE Object"
Case msoLinkedPicture
ShapeTypeName = "Linked Picture"
Case msoMedia
ShapeTypeName = "Media"
Case msoOLEControlObject
ShapeTypeName = "OLE Control Object"
Case msoPicture
ShapeTypeName = "Picture"
Case msoPlaceholder
ShapeTypeName = "Placeholder"
Case msoScriptAnchor
ShapeTypeName = "Script Anchor"
Case msoShapeTypeMixed
ShapeTypeName = "Mixed"
Case msoTable
ShapeTypeName = "Table"
Case msoTextBox
ShapeTypeName = "TextBox"
Case msoTextEffect
ShapeTypeName = "Text Effect"
Case msoMedia
ShapeTypeName = "Media"
Case Else
ShapeTypeName = "Unknown"
End Select
End Function
Summary
This code helps identify exactly what kind of objects are placed on your slide. I hope this serves as a useful reference for your projects.
