Automating Powerpoint
Google
Web www.mahipalreddy.com

 

The articles uses Microsoft Powerpoint 9.0 Object library that come with Microsoft Office 2000. If you are using earlier or later versions of Microsoft office, make sure you reference the appropriate object libraries.

The article explains how to use Powerpoint objects (programatically using VBA) from Microsoft excel.
(It shouldn't make any difference if you are trying to automate powerpoint from a VB application, except that you may need to reference 'Microsoft Office Object library' to use the code as it is). You can also use the same code from within the powerpoint macros, in which case you don't need to reference the powerpoint object library and also don't need to instantiate the application object.


To get cracking with the code,
-Open Microsoft Excel,then goto Visual Basic Editor(Alt+F11) and Insert a Module
-Goto tools-->References and add  Microsoft Powerpoint 9.0 object library and Microsoft Graph 9.0 Object Library
-add a Procedure to the module to test the code bits from the article.

The first thing is to declare the object variables

   Dim oPPTApp As PowerPoint.Application
   Dim oPPTFile As PowerPoint.Presentation
   Dim oPPTShape As PowerPoint.Shape
   Dim oPPTSlide As PowerPoint.Slide
   Dim oGraph As Graph.Chart
   Dim oAxis As Graph.Axis
   Dim SlideNum As Integer

Then instantiate the powerpoint application and make it visible

   Set oPPTApp = CreateObject("PowerPoint.Application")
   oPPTApp.Visible = msoTrue

now, either open an existing presentation from the disk or add a new presentation and save it

   'opening an existing presentation
   Set oPPTFile = oPPTApp.Presentations.Open(Filename:=myTemplateDirectory & "\" & myTemplateFile)

   'adding a new presentation and saving it
   Set oPPTFile = oPPTApp.Presentations.Add(msoTrue)  
   oPPTFile.SaveAs "c:\mypres.ppt" 

Accessing an existing slide or adding a new slide

   'accessing an existing slide by its number
   SlideNum = 1
   Set oPPTSlide = oPPTFile.Slides(SlideNum).Select
   oPPTSlide.ColorScheme.Colors(ppBackground) = vbYellow

  'adding a new slide and accessing it
  Set oPPTSlide = oPPTFile.Slides.Add(1, ppLayoutText)
  'the 1st parameter of the ADD methos used above specifies the index position of the new slide to be added. so
  'if you have say, 10 slides and you want to add the new one before 6th slide, you say like this......
  'Set oPPTSlide = oPPTFile.Slides.Add(6, ppLayoutText)

   oPPTSlide.ColorScheme.Colors(ppBackground) = vbYellow

Accessing an existing shape or adding a new shape to a powerpoint slide

Everything you add to a slide is a shape in powerpoint, for eg. a text box, a table or a rectangle is a shape. Chart and Organization charts are OLE objects embedded inside the shapes.
so, for example if you need to change some text, first you need to access the shape having that text, then change the text inside it.
And most of the shapes will have a 'TextFrame' associated with them, which means that we can add some text to it even if it's not a text box!
The following code accesses a textbox and sets its text to "My text string"

   'accessing an existing shape on a slide by its name.
   'say, our shape is a textbox on the 1st slide and its name is  "Report_title"
   
   SlideNum = 1
   Set oPPTShape = oPPTFile.Slides(SlideNum).Shapes("Report_title")
   oPPTShape.TextFrame.TextRange.Text = "My text string"

   'adding a new shape and add some text to it
   'first add a blank slide then add a text box to it

   Set oPPTSlide = oPPTFile.Slides.Add(1, ppLayoutBlank)
   oPPTSlide.Shapes.AddTextbox msoTextOrientationHorizontal, 10, 20, 300, 5
   oPPTSlide.Shapes(1).TextFrame.TextRange.Text = "I'm a new shape on a new slide with some text"

Adding a chart to your Presentation

   'ADD A BLANK SLIDE TO THE PRESENTATION
   Set oPPTSlide = oPPTFile.Slides.Add(1, ppLayoutBlank)
    
   'ADD A TEXT BOX AND SOME TEXT TO IT
   oPPTSlide.Shapes.AddTextbox msoTextOrientationHorizontal, 30, 20, 600, 20
   With oPPTSlide.Shapes(1).TextFrame.TextRange
      .Text = "My new Chart below!"
      .Font.Color = vbRed
      .Font.Underline = msoTrue
   End With
   'NOW ADD GRAPH OBJECT TO THE SLIDE
   oPPTSlide.Shapes.AddOLEObject 30, 70, 600, 400, "MSGraph.Chart"

Adding an Organization chart to your Presentation

   'ADD A BLANK SLIDE TO THE PRESENTATION
   Set oPPTSlide = oPPTFile.Slides.Add(1, ppLayoutBlank)
    
   'ADD A TEXT BOX AND SOME TEXT TO IT
   oPPTSlide.Shapes.AddTextbox msoTextOrientationHorizontal, 30, 20, 600, 20
   With oPPTSlide.Shapes(1).TextFrame.TextRange
     .Text = "My new Organisation Chart below!"
     .Font.Color = vbRed
     .Font.Underline = msoTrue
   End With
   'NOW ADD ORGANISATION CHART OBJECT TO THE SLIDE
   oPPTSlide.Shapes.AddOLEObject 100, 150, 400, 300, "OrgPlusWOPX.4"

Although you can automate a powerpoint graph, there is no way of automating organization chart using VBA, at least until Office 2000 and Office XP.


Adding a Table to your Presentation

   'ADD A BLANK SLIDE TO THE PRESENTATION
   Set oPPTSlide = oPPTFile.Slides.Add(1, ppLayoutBlank)
    
   'ADD A TEXT BOX AND SOME TEXT TO IT
   oPPTSlide.Shapes.AddTextbox msoTextOrientationHorizontal, 30, 20, 600, 20
   With oPPTSlide.Shapes(1).TextFrame.TextRange
     .Text = "My new Table below!"
     .Font.Color = vbRed
     .Font.Underline = msoTrue
   End With
   'NOW ADD A TABLE WITH 2 ROWS AND 2 COLUMNS TO THE SLIDE

   oPPTSlide.Shapes.AddTable 2, 2, 100, 150, 500, 100
   Set oPPTShape = oPPTFile.Slides(1).Shapes(2)
    
   'ADD SOME TEXT TO THE TABLE
   oPPTShape.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "Row1- Col1"
   oPPTShape.Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = "Row1- Col2"
   oPPTShape.Table.Cell(2, 1).Shape.TextFrame.TextRange.Text = "Row2- Col1"
   oPPTShape.Table.Cell(2, 2).Shape.TextFrame.TextRange.Text = "Row2- Col2"
   
   oPPTShape.Table.Columns(1).Width = 200
   oPPTShape.Table.Columns(2).Width = 400

Looping through all the slides in a powerpoint presentation and all the shapes in a slide

   For Each oPPTSlide In oPPTFile.Slides
     For Each oPPTShape In oPPTSlide.Shapes
       MsgBox oPPTShape.Name
     Next
   Next

Running slideshow with an interval of 2 seconds

   'Add some slides to the presentation before running this slide show
   With oPPTFile.Slides.Range.SlideShowTransition
       .EntryEffect = ppEffectRandom
       .AdvanceOnTime = msoTrue
       .AdvanceTime = 2 ' 2 seconds per slide
   End With
   With oPPTFile.SlideShowSettings
       .ShowType = ppShowTypeKiosk
       .LoopUntilStopped = msoTrue
       .RangeType = ppShowAll
       .AdvanceMode = ppSlideShowUseSlideTimings
       .Run
   End With

Delete a shape on a powerpoint slide

   oPPTFile.Slides(1).Shapes(2).Delete

Delete a slide from the presentation

   oPPTFile.Slides(1).Delete

Top

 
www.mahipalreddy.com
Terms and Conditions of Use
Copyright � 2004 - 2006 Mahipal Padigela. All rights reserved.