[Excel VBA] How to Automatically Create a PowerPoint Presentation

Creating PowerPoint reports using data or charts analyzed in Excel is a common task in many business scenarios. By using VBA, you can automate the creation of these PowerPoint presentations.

In this article, I will explain the most basic workflow: launching PowerPoint from Excel VBA, adding slides and text boxes to a new presentation, and saving the file.

目次

[Important] Setting References (Recommended)

To control PowerPoint efficiently with VBA, I recommend setting up References to use “Early Binding.”

  1. Open the VBE (Alt + F11) and click Tools > References in the menu.
  2. Find Microsoft PowerPoint XX.0 Object Library in the list, check the box, and click OK. (The “XX.0” depends on your PowerPoint version).

This setting enables the use of PowerPoint specific terms (constants) within VBA, making the code much easier to write and read.

Completed VBA Code

Below is the VBA code that creates a new PowerPoint presentation and adds a title slide.

' Reference: Microsoft PowerPoint XX.0 Object Library
Sub CreatePowerPointPresentation()

    ' Declare variables
    Dim ppApp As PowerPoint.Application
    Dim ppPres As PowerPoint.Presentation
    Dim ppSld As PowerPoint.Slide
    Dim titleShape As PowerPoint.Shape

    ' --- 1. Launch PowerPoint Application ---
    Set ppApp = New PowerPoint.Application
    ppApp.Visible = True ' Make the application visible

    ' --- 2. Add a New Presentation ---
    Set ppPres = ppApp.Presentations.Add

    ' --- 3. Add a Slide ---
    ' Add the 1st slide using the "Blank" layout
    Set ppSld = ppPres.Slides.Add(1, ppLayoutBlank)
    
    ' --- 4. Add a Textbox and Format It ---
    ' AddTextbox(Orientation, Left, Top, Width, Height)
    Set titleShape = ppSld.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 150, 860, 200)
    
    ' Manipulate the text string inside the textbox
    With titleShape.TextFrame.TextRange
        .Text = "Auto-Generated Title via Excel VBA"
        .Font.Name = "Arial"
        .Font.Size = 60
        .Font.Bold = True
        .ParagraphFormat.Alignment = ppAlignCenter
    End With

    ' --- 5. Save and Quit ---
    ppPres.SaveAs ThisWorkbook.Path & "\VBA_Generated_Presentation.pptx"
    ppApp.Quit

    ' --- 6. Release Objects ---
    Set titleShape = Nothing
    Set ppSld = Nothing
    Set ppPres = Nothing
    Set ppApp = Nothing
    
    MsgBox "PowerPoint presentation creation complete."

End Sub

Explanation of Key Points

1. Launching the PowerPoint Application

Set ppApp = New PowerPoint.Application
ppApp.Visible = True

Use New PowerPoint.Application to create the PowerPoint application object. Just like with Word automation, setting .Visible = True allows you to watch the process on the screen.

2. Adding Presentation and Slides

Set ppPres = ppApp.Presentations.Add
Set ppSld = ppPres.Slides.Add(1, ppLayoutBlank)
  • .Presentations.Add: Creates a new empty presentation (equivalent to a .pptx file).
  • .Slides.Add(Index, Layout): Adds a new slide to the presentation. Index: 1 means adding it as the first slide.
  • Layout: ppLayoutBlank is a constant specifying a blank layout. You can specify various layouts, such as ppLayoutTitle (Title only).

3. Adding a Textbox (.Shapes.AddTextbox)

Set titleShape = ppSld.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 150, 860, 200)

All figures on a slide (textboxes, images, charts, etc.) are managed by the Shapes collection. The .AddTextbox method adds a textbox. The arguments specify the orientation, position (Left, Top), and size (Width, Height) in “points.”

4. Entering Text and Formatting

With titleShape.TextFrame.TextRange
    .Text = "..."
    .Font.Size = 60
    .ParagraphFormat.Alignment = ppAlignCenter
End With

To manipulate text inside a Shape, you basically access the .TextFrame.TextRange object.

  • .Text: Sets the string to be displayed.
  • .Font: Allows you to manipulate font properties (such as .Size, .Name, .Bold).
  • .ParagraphFormat: Allows you to manipulate paragraph properties. ppAlignCenter is the constant for center alignment.

5. Saving and Quitting

ppPres.SaveAs "File Path"
ppApp.Quit

Use .SaveAs to save the presentation as a file, and always make sure to use .Quit at the end to close the PowerPoint application.

Summary

The procedure for controlling PowerPoint from Excel VBA is very similar to Word automation.

  1. Create the Application object.
  2. Add a Presentation object (the file).
  3. Add a Slide object (the page).
  4. Add a Shape object (such as a textbox).
  5. Set content and formatting using the TextRange object inside the Shape.

Understanding this object hierarchy is the key to PowerPoint automation. By applying these basics, automatically generating full presentations based on Excel data is within reach.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次