Thursday, 6 September 2012

VB.NET - PowerPoint - How to create simple PowerPoint file using VB.NET


Watch this example on YouTube:

1. First Add Reference to Microsoft PowerPoint (from COM tab)










 

 2. Go to Properties of Microsoft.Office.Interop.PowerPoint and ensure that Embed Interop Types is set to False:





 

 3. Import Microsoft.Office.Interop.PowerPoint

Imports PowerPoint = Microsoft.Office.Interop.PowerPoint

4. Add button and copy the following code to click event
    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ppApplication As PowerPoint.ApplicationClass = Nothing
        Dim ppPresentation As PowerPoint.Presentation = Nothing
        Dim ppSlide As PowerPoint.Slide = Nothing
        Dim ppTextRange As PowerPoint.TextRange = Nothing

        Try
            Dim fileTest As String = "C:\Temp\PPTest\Test.pptx"
            ppApplication = New Microsoft.Office.Interop.PowerPoint.ApplicationClass()
            ppPresentation = ppApplication.Presentations.Add
            ppSlide = ppPresentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank)
            ppSlide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 20, 312, 489, 41).TextEffect.Alignment = Microsoft.Office.Core.MsoTextEffectAlignment.msoTextEffectAlignmentRight

            ppTextRange = ppSlide.Shapes(1).TextFrame.TextRange
            ppTextRange.Text = "This is my first PowerPoint file - Hellow World!"
            ppTextRange.Font.Size = 28
            ppTextRange.Font.Name = "HleveticaNeue LT 55 Roman"
            ppTextRange.Font.Color.RGB = RGB(102, 153, 0)
            ppPresentation.SaveAs(fileTest)

        Catch ex As Exception
            ' handle errors here
        Finally
            ' clean up
            ppSlide = Nothing
            If Not ppPresentation Is Nothing Then
                ppPresentation.Close()
                ppPresentation = Nothing
            End If
            If Not ppApplication Is Nothing Then
                ppApplication.Quit()
                ppApplication = Nothing
            End If
            GC.Collect()
            GC.WaitForPendingFinalizers()

        End Try

1 comment:

  1. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing. psiprograms.com

    ReplyDelete