Wednesday, 17 October 2012

VB.NET - PowerPoint - How to fix erro "Shapes (unknown member) : Integer out of range. 0 is not in the valid range of 1 to 0."

ex.Message = "Shapes (unknown member) : Integer out of range. 0 is not in the valid range of 1 to 0."

Watch Online
Replace (in red)

    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 slide!"
            ppTextRange.Font.Size = 28
            ppTextRange.Font.Name = "HleveticaNeue LT 55 Roman"
            ppTextRange.Font.Color.RGB = RGB(102, 153, 0)

            ppSlide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 20, 312, 489, 41).TextEffect.Alignment = Microsoft.Office.Core.MsoTextEffectAlignment.msoTextEffectAlignmentRight
            ppSlide = ppPresentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank)

            ppTextRange = ppSlide.Shapes(0).TextFrame.TextRange
            ppTextRange.Text = "This is my second slide!"
            ppTextRange.Font.Size = 28
            ppTextRange.Font.Name = "HleveticaNeue LT 55 Roman"
            ppTextRange.Font.Color.RGB = RGB(102, 153, 0)


with

            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 slide!"
            ppTextRange.Font.Size = 28
            ppTextRange.Font.Name = "HleveticaNeue LT 55 Roman"
            ppTextRange.Font.Color.RGB = RGB(102, 153, 0)

           
            ppSlide = ppPresentation.Slides.Add(2, 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 second slide!"
            ppTextRange.Font.Size = 28
            ppTextRange.Font.Name = "HleveticaNeue LT 55 Roman"
            ppTextRange.Font.Color.RGB = RGB(102, 153, 0)


            ppPresentation.SaveAs(fileTest)

No comments:

Post a Comment