Excel VBA PowerPoint
Using VBA we can automate the work we do for PowerPoint, but first in order to use VBA code or snippets to work in powerpoint first work through the security options in PowerPoint to enable all macros and then we can use PowerPoint VBA reference for macros in MS PowerPoint.
The beauty of VBA is that we can reference other Microsoft products like “Microsoft Word” and “Microsoft PowerPoint”. We usually create reports in excel and then create PowerPoint presentations. All the excel users usually spend a considerable amount of time to prepare the presentation from excel data and reports. If you are spending a considerable amount of time to prepare PowerPoint presentations this tutorial will show you how to create a PowerPoint presentation from excel itself using VBA Coding.
Enable Powerpoint Object Model
Step 1: Open VBA Editor and then, Go to Tools and References.
Step 2: Now you will see all the references to the VBA Project. Scroll down and select “Microsoft PowerPoint 15.0 Object Library”.
Step 3: Click on Ok. Now we can access PowerPoint from excel.
VBA Tutorial to Create PowerPoint Presentation
We can create PPT in two way one by using “Early Binding” and another one is using “Late Binding”. We will show you how to create a powerpoint presentation by using “Early Binding” technique.
Usually, from excel we prepare presentations based on charts and interpretation of the charts. So for this purpose, I have created some simple charts and interpretation in the same worksheet.
Step 1: Start the subprocedure. Now to access PowerPoint we have already enabled PowerPoint object model in the earlier steps, now to access this we need to declare the variable as PowerPoint.Application.
Sub PPT_Example() Dim PPApp As PowerPoint.Application End Sub
Step 2: To add the presentation to PowerPoint we need to declare a variable as PowerPoint.Presentation.
Dim PPPresentation As PowerPoint.Presentation
Step 3: After adding the presentation to the PowerPoint we need to add Slide. To declare the variable as PowerPoint.Slide
Dim PPSlide As PowerPoint.Slide
Step 4: Once the slide is added to the PowerPoint we need to make use of shapes in the PowerPoint i.e. text boxes. To declare a variable as PowerPoint.Shape
Dim PPShape As PowerPoint.Shape
Step 5: Now to access all the charts in the worksheet we need to declare the variable as Excel.ChartObjects.
Dim PPCharts As Excel.ChartObject
Ok, to start off the proceedings these variables are enough.
Step 6: Now we need to launch the PowerPoint from excel. Since it is an external object we need to set this as a new powerpoint.
Set PPApp = New PowerPoint.Application
This will launch the new PowerPoint from excel.
Step 7: Now the variable PPApp is equal to the PowerPoint we have launched. Now make this PowerPoint visible and maximize the window.
PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized
At this moment just run the code using F5 key or manually, you should see PowerPoint app launched like the below one.
Step 8: Now we need to add a presentation to the PowerPoint app we have launched.
Set PPPresentation = PPApp.Presentations.Add
Now we should see the PowerPoint presentation like this.
Step 9: After adding the presentation we need to add a slide.
Set PPSlide = PPPresentation.Slides.Add(1, ppLayoutTitleOnly)
Now this will add the title slide like the below.
Step 10: Now we have more than one chart in the worksheet we need to loop through each chart and paste in the presentation. Below is the code to copy and paste the chart as well as interpretation.
Below is the complete code for you.
Sub PPT_Example() Dim PPApp As PowerPoint.Application Dim PPPresentation As PowerPoint.Presentation Dim PPSlide As PowerPoint.Slide Dim PPShape As PowerPoint.Shape Dim PPCharts As Excel.ChartObject Set PPApp = New PowerPoint.Application PPApp.Visible = msoCTrue PPApp.WindowState = ppWindowMaximized 'Add Presentation Set PPPresentation = PPApp.Presentations.Add 'Loop through each chart in the Excel and paste into the PowerPoint For Each PPCharts In ActiveSheet.ChartObjects PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutText PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count Set PPSlide = PPApp.ActivePresentation.Slides(PPApp.ActivePresentation.Slides.Count) 'Copy the chart and paste in Powerpoint PPCharts.Select ActiveChart.ChartArea.Copy PPSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select 'Add heading to the slide PPSlide.Shapes(1).TextFrame.TextRange.Text = PPCharts.Chart.ChartTitle.Text 'Allignment of the chart PPApp.ActiveWindow.Selection.ShapeRange.Left = 15 PPApp.ActiveWindow.Selection.ShapeRange.Top = 125 PPSlide.Shapes(2).Width = 200 PPSlide.Shapes(2).Left = 505 'Add interpretation If InStr(PPSlide.Shapes(1).TextFrame.TextRange.Text, "Region") Then PPSlide.Shapes(2).TextFrame.TextRange.Text = Range("K2").Value & vbNewLine PPSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("K3").Value & vbNewLine) 'Else if the chart is the "Renewable" consumption chart, then enter the appropriate comments ElseIf InStr(PPSlide.Shapes(1).TextFrame.TextRange.Text, "Month") Then PPSlide.Shapes(2).TextFrame.TextRange.Text = Range("K20").Value & vbNewLine PPSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("K21").Value & vbNewLine) PPSlide.Shapes(2).TextFrame.TextRange.InsertAfter (Range("K22").Value & vbNewLine) End If 'Now let's change the font size of the callouts box PPSlide.Shapes(2).TextFrame.TextRange.Font.Size = 16 Next PPCharts End Sub
This has been a guide to VBA PowerPoint Tutorial. Here we learn how to create a powerpoint presentation by using “Early Binding” technique in vba code along with examples and a downloadable template. Below are some useful excel articles related to VBA –