PowerPoint VBA: Selecting a Table on an Active Slide

This PowerPoint VBA macro selects a table on an active slide.

Macro Example

Sub SelectTable()

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Select
    End With
  Next shp

End Sub

Description

Variable Declaration

Sub...

  Dim shp As Shape

The 3rd line declares an object variable named shp that refers to a Shape object.

The Shape object represents a placeholder, shape, freeform, OLE object or picture.

obj-PowerPoint-Shape

Starts Looping Through All Shapes

Sub...

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes

The 5th line starts to loop through all the elements (Shape) of a collection (Shapes).

(Application.)ActiveWindow

  For Each shp In ActiveWindow...

The Application.ActiveWindow property returns the DocumentWindow object that represents an active document window.

obj-PowerPoint-Application-ActiveWindow

The ActiveWindow property is a member of the PowerPoint.Global class, so we can omit the Application property.

obj-PowerPoint-Global-ActiveWindow

ActiveWindow.Selection

  For Each shp In ActiveWindow.Selection...

The ActiveWindow.Selection (DocumentWindow.Selection property) returns the Selection object that represents the selection in the document window.

obj-PowerPoint-DocumentWindow-Selection

Selection.SlideRange

  For Each shp In ActiveWindow.Selection.SlideRange...

The Selection.SlideRange property returns the SlideRange object that represents a range of selected slides.

obj-PowerPoint-Selection-SlideRange

In this macro it represents an active slide.

SlideRange.Shapes

  For Each shp In ActiveWindow.Selection.SlideRange.Shapes

The SlideRange.Shapes property returns a Shapes collection object that represents all the shapes that have been placed on a slide.

obj-PowerPoint-SlideRange-Shapes

Starts the With structure

Sub...

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp

The 6th line starts the With…End With statement.
This statement tells PowerPoint that any action apply to the shp (Shape object).

Checks If the Shape Has a Table and…

Sub...

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Select

The 7th line checks if the shape is a table, and if so selects it.

Shape.HasTable

    With shp
      If .HasTable...

The shp.HasTable (Shape.HasTable property) returns whether the shape is a table.

Shape.Select

    With shp
      If .HasTable Then .Select

If so, select it by the shp.Select (Shape.Select method).

Loops Back

Sub...

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Select
    End With
  Next shp

The 9th line loops back to evaluate the next shape.

After every shape is evaluated, this macro ends.

Sub SelectTable()

  Dim shp As Shape
 
  For Each shp In ActiveWindow.Selection.SlideRange.Shapes
    With shp
      If .HasTable Then .Select
    End With
  Next shp

End Sub

Properties

http://www.relief.jp/itnote/archives/powerpoint-vba-select-table-on-active-slide.php

Apply to

  • PowerPoint 2013
  • PowerPoint 2010
  • PowerPoint 2007