Saturday, July 08, 2006

VBA - Creating PowerPoint Presentation

Most often we will come across a scenario where powerpoint slides need to be created automatically.

Here is a sample & simple code to do that. This code is created using VBA (Excel 2000)

Sub Create_PowerPoint_Slides()

On Error GoTo Err_PPT

Dim oPA As PowerPoint.Application
Dim oPP As PowerPoint.Presentation
Dim oPS As PowerPoint.Slide
Dim oShape As PowerPoint.Shape
Dim sPath As String
Dim sFile As String
Dim i1 As Integer


sPath = "C:\"
sFile = "MyfileName"

Set oPA = New PowerPoint.Application
oPA.Visible = msoTrue

Set oPP = oPA.Presentations.Add(msoTrue)

For i1 = 1 To 10
oPP.Slides.Add 1, ppLayoutBlank
Next i1

Set oPS = oPP.Slides(1)
Set oShape = oPS.Shapes.AddTextbox(msoTextOrientationHorizontal, 140#, 246#, 400#, 36#)
oShape.TextFrame.WordWrap = msoTrue

oShape.TextFrame.TextRange.Text = "Comments For File : " & sFile
With oShape
.Fill.Visible = msoTrue
.Fill.Solid
.Fill.ForeColor.RGB = RGB(204, 255, 255)
.Line.Weight = 3#
.Line.Visible = msoTrue
.Line.ForeColor.SchemeColor = ppForeground
.Line.BackColor.RGB = RGB(255, 255, 255)
End With


oPP.SaveAs sPath & sFile & ".ppt"
oPP.Close
oPA.Quit

If Not oPS Is Nothing Then Set oPS = Nothing
If Not oPP Is Nothing Then Set oPP = Nothing
If Not oPA Is Nothing Then Set oPA = Nothing



Err_PPT:
If Err <> 0 Then
MsgBox Err.Description
Err.Clear
Resume Next
End If

End Sub

7 comments:

  1. I just tried your code, but VBA says that Powerpoint.application is a user-defined type not yet defined. What should I do?

    ReplyDelete
  2. You need to add a reference to Powerpoint library. You can do that from VBE-->Tools-->References select the Microsoft Office Powerpoint checkbox and save the project

    ReplyDelete
  3. How can i add a rectangular shape and fill color only upto a given percent

    ReplyDelete
  4. I need to do this from an ASP Classic website. Is there a VBScript version of this?

    ReplyDelete
  5. Anonymous7:09 PM

    Phillip, the code should also work for VBScript version.

    Try replacing the earlybidnbing with late binding using CreateObject.

    ReplyDelete
  6. Anonymous7:10 PM

    @Maheswaran

    You should first add the shapes and specify the co-oordinates. Then you can specify the fill.

    I am not sure about 'certain percent'. Are you trying for transparency

    ReplyDelete
  7. Anonymous3:54 AM

    Power point presentations are usually used in presenting proposals or reports. In both cases, the presentor can choose to use either a formal or interactive approach. Formal presentations refer to those which involve a first part which is dedicated solely to presenting without interaction with the audience and audience interaction or participation is scheduled after the presentation. On the other hand, interactive presentations are those which encourage or solicit interaction or participation from the audience during the presentation. Specifically, the audience is involved in every slide of the presentation.

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.