Ty Anderson

PowerPoint 2013 add-in development in Visual Studio: Application and base objects

Ever wonder why each of the Office apps have the colors they do? For example Excel is green. I think that makes sense because accountants and financial-types dig Excel in a big way.

Word is blue and the only guess I have for this color assignment is that the idea of writing makes most people sad. Outlook is yellow… maybe to warn us that spending too much time obsessing over email is a bad for our health. But I don’t really know.

PowerPoint is orange-ish red… and I know exactly why. PowerPoint is the red-headed step-child of Office development. Sure, it’s part of the family but it’s a little weird and we tend to ignore it (from a solution development perspective.)

Today we will begin giving PowerPoint some love and attention by explaining its base objects and providing code samples.

PowerPoint base objects

Like all Office apps, the main object in the PowerPoint object model is the Application object. Underneath it we have a few key objects that will allow us to achieve most of our development goals. Here is summary of PowerPoint’s base objects:

  • Application: The PowerPoint application, the object that ties all objects together.
  • Presentation: This is the PowerPoint file (.pptx, etc). It contains all objects in a PowerPoint file. There is, of course, corresponding Presentations collection that contains all presentation files.
  • Slide: This is a slide in a PowerPoint file. It lives in the Slides collection.
  • Shape: Shapes of all types.
  • Selection: This object contains the current selection in the active PowerPoint window.

I feel silly explaining these objects. Really, I just wanted to list them so you know where we are heading next. Their names are self-explanatory.

Application object

The Application object not only provides access to key child objects like those just mentioned, it also contains useful properties that let you control how PowerPoint behaves and looks. The sample code below takes advantage of this fact.

Dim appPath As String
 
Private Sub SetSomeApplicationSettings()
 
    appPath = PowerPointApp.Path
 
    If PowerPointApp.Visible Then
        PowerPointApp.DisplayAlerts = False
        PowerPointApp.DisplayGridLines = True
        PowerPointApp.Height = 600
        PowerPointApp.Width = 800
        PowerPointApp.Left = 200
        PowerPointApp.Top = 250
        PowerPointApp.ShowStartupDialog = Office.MsoTriState.msoFalse
    End If
End Sub

In this sample, I show how to grab the application path and store it for later (in case you need it). In addition, I position PowerPoint in a specific location… but, I only do this if PowerPoint’s window is visible. Lastly, I prevent the startup dialog from displaying.

Presentation object

We working with the Presentation object, you are working with PowerPoint files. Thus, it’s good to know how to access them and manipulate them.

Enumerate presentations

To efficiently process files, you need to enumerate the Presentations collection and work with each Presentation object. This sample loops through the collection and performs a SaveAs method.

Private Sub EnumeratePresentations()
    Dim ppts As PowerPoint.Presentations = PowerPointApp.Presentations
 
    Dim path As String = Environment.GetFolderPath( _
        Environment.SpecialFolder.CommonDesktopDirectory)
 
    For i As Integer = ppts.Count To 1 Step -1
        Dim p As PowerPoint.Presentation = ppts(i)
        p.SaveAs(path & "\" & i.ToString() & ".pptx")
        p.Close()
        Marshal.ReleaseComObject(p)
    Next
 
    Marshal.ReleaseComObject(ppts)
End Sub

There are better examples in the world than saving to the desktop… like changing their theme or inserting title slides.

Create a new presentation

Creating a new presentation is an easy, two steps process as this sample demonstrates:

Private Function CreateNewPresentation(savePath As String) As _
    PowerPoint.Presentation
 
    Dim newPPT As PowerPoint.Presentation = Nothing
    Dim ppts As PowerPoint.Presentations = PowerPointApp.Presentations
 
    newPPT = ppts.Add()
    Marshal.FinalReleaseComObject(ppts)
    Return newPPT
End Function

Step one is to create Presenation object. Step two is call the Add method of the Presentations collection. Boom! A new presentation exists.

Open a presentation (existing or create a new one from template)

This sample is a “twofer“. Whether you want to open an existing file… or… create new presentation from a template file (.potx or .pot), you do the same thing:

Private Function OpenNewPPT(filePath As String) As  _
    PowerPoint.Presentation
 
    Dim newPPT As PowerPoint.Presentation = Nothing
    Dim ppts As PowerPoint.Presentations = PowerPointApp.Presentations
 
    newPPT = ppts.Open(filePath)
    Marshal.FinalReleaseComObject(ppts)
    Return newPPT
End Function

You create a new Presentation object then call the Open method of the Presentations object. The Open method does need to know the path to the file you want to open.

Slide Object

Excel has worksheets, Word has pages, and PowerPoint has slides. Slides contain the content of a presentation.

Enumerate slides

Looping through slides is a useful skill. This code example moves through the Slides collection and changes the theme of each and every Slide in the collection.

Private Sub EnumerateSlides(ppt As PowerPoint.Presentation, themeName As String)
    Dim i As Integer
    Dim slides As PowerPoint.Slides = ppt.Slides
 
    For i = 1 To slides.Count
        Dim slide As PowerPoint.Slide = TryCast(slides(i), PowerPoint.Slide)
        If slide IsNot Nothing Then
            slide.ApplyTheme(themeName)
            Marshal.ReleaseComObject(slide)
        End If
    Next
 
    Marshal.ReleaseComObject(slides)
End Sub

This example can be useful if you are stuffing a presentation with slides from several sources with varying themes. In one quick bit of code, all the slides will contain the same look and feel.

Create a slide

Creating a slide is not as simple as adding a new Slide object to the Slides collection. First, you must create a CustomLayout object. Then you, add the slide and assign the layout to it.

Private Sub AddNewSlide(ppt As PowerPoint.Presentation)
    Dim layout As PowerPoint.CustomLayout
    Dim slide As PowerPoint.Slide
    Dim slides As PowerPoint.Slides
    Dim presentation As PowerPoint.Presentation
 
    presentation = PowerPointApp.ActivePresentation
    slide = presentation.Slides(1)
    layout = slide.CustomLayout
    Marshal.ReleaseComObject(presentation)
    Marshal.ReleaseComObject(slide)
 
    slides = ppt.Slides
    slide = slides.AddSlide(0, layout)
 
    Marshal.ReleaseComObject(slide)
    Marshal.ReleaseCo

In the code above, I use the previous slide’s CustomLayout and assign it to the new slide. It all happens in the call to AddSlide.

Delete a slide

If you add slides, you will delete slides. In this sample, I show how to loop through the slides collection in search of a specific slide. If and when the code finds that slide, it deletes it.

Private Sub DeleteSlide(ppt As PowerPoint.Presentation, slideName As String)
    Dim i As Integer
    Dim slides As PowerPoint.Slides
 
    PowerPointApp.DisplayAlerts = False
    slides = ppt.Slides
 
    For i = 1 To slides.Count
        Dim slide As PowerPoint.Slide = TryCast(slides(i), PowerPoint.Slide)
        If slide.Name = slideName Then
            slide.Delete()
            Marshal.ReleaseComObject(slide)
            Exit For
        End If
        Marshal.ReleaseComObject(slide)
    Next
 
    PowerPointApp.DisplayAlerts = True
    Marshal.ReleaseComObject(slides)
End Sub

Shape object

In PowerPoint, you don’t work with free form text so much as you work with shapes. These shapes illustrate key concepts and often utilize text to do so. The point is, slides contain lots of shapes.

Enumerate shapes

We use the Shapes collection to access each shape on the slide. Here, I move each shape 1 point to the right (mind you, a point, not a pixel, and 1 pt = 1/72 inch).

Private Sub EnumerateShapes(slide As PowerPoint.Slide)
    Dim i As Integer
    Dim shapes As PowerPoint.Shapes = slide.Shapes
 
    For i = 1 To shapes.Count
        Dim shape As PowerPoint.Shape = TryCast(shapes(i), PowerPoint.Shape)
        If shape IsNot Nothing Then
            shape.Left = shape.Left + 1
            Marshal.ReleaseComObject(shape)
        End If
    Next
 
    Marshal.ReleaseComObject(shapes)
End Sub

Add a shape

To add a shape in PowerPoin, you call the AddShape method of the Shapes collection. When you make this call, you need to specify what type of Shape you want.

Private Sub AddShapeToSlide(slide As PowerPoint.Slide)
    Dim shape As PowerPoint.Shape
    Dim shapes As PowerPoint.Shapes = slide.Shapes
 
    shape = shapes.AddShape(Office.MsoAutoShapeType.msoShape7pointStar, _
        50, 50, 50, 50)
 
    Marshal.ReleaseComObject(shapes)
    Marshal.ReleaseComObject(shape)
End Sub

This sample creates a 7-point star shape and sets its Top, Left, Height, and Width properties.

Delete a shape

Deleting a shape is easy. Finding the shape you want to delete is not. In this sample, I show how to loop through the shapes collection and look for the shape that contains a certain text value. If we find it, we delete it.

Private Sub DeleteShape(slide As PowerPoint.Slide, containsText As String)
    Dim i As Integer
    Dim shapes As PowerPoint.Shapes = slide.Shapes
 
    For i = shapes.Count To 1 Step -1
        Dim shape As PowerPoint.Shape = TryCast(shapes(i), PowerPoint.Shape)
        If shape.TextFrame.TextRange.Text = containsText Then
            shape.Delete()
        End If
        Marshal.ReleaseComObject(shape)
    Next
    Marshal.ReleaseComObject(shapes)
End Sub

I wish there were a better way but it seems looping is not only fun, it is the only way.

Selection object

The Selection object represents the active PowerPoint window’s selected object. It resides under the ActiveWindow as you can see in this example:

Private Sub ChangeSelectionText(newText As String)
    Dim window As PowerPoint.DocumentWindow
    Dim selection As PowerPoint.Selection
    Dim range As PowerPoint.TextRange
 
    window = PowerPointApp.ActiveWindow
    selection = window.Selection
    range = selection.TextRange
    range.Text = newText
 
    Marshal.ReleaseComObject(range)
    Marshal.ReleaseComObject(selection)
    Marshal.ReleaseComObject(window)
End Sub

Notice I access the Selection’s TextRange property and change its text. The Selection object also has:

***

This is only the beginning of our samples. PowerPoint is underappreciated as an add-in target. This means it is ripe for the picking as the market is underserved. Meaning, the PowerPoint user base is being ignored and treated like, well, a red-headed step child (no offense intended to anyone is 1) red-headed and 2) a step-child).

Available downloads:

This sample Outlook add-in was developed using Add-in Express for Office and .net:

VB.NET PowerPoint Base Objects add-in

You may also be interested in:

Post a comment

Have any questions? Ask us right now!