Automate powerpoint - create multiple presentations based on data from a database using Visual Basic .NET

Hi!

I'm working on a solution that will create multiple PPT-presentations, each presentation about 15-25 slides and on each slide about 5-15 shapes (graphs and tables). Every graph/table will be updated with data from tab-separated textfiles.

After about 40-50 created reports, PowerPoint crashes and generates an error message saying something like "The RPC-server is unavailable.".

My question is: how do I best declare the objects and should this be in or outside the loop with the reports? Today the code looks something like:

Public Class CreatePPTReports
    Dim oApp As Microsoft.Office.Interop.Powerpoint.Application
    Dim oPres As Microsoft.Office.Interop.PowerPoint.Presentation

    Public Sub CreatePPT()

        'reports is the array containing the reports to be created
        For Each itm As String In Reports
            Dim oPreses As Microsoft.Office.Interop.PowerPoint.Presentations
            Dim oPres As Microsoft.Office.Interop.powerPoint.Presentation
            Dim oSlides As Microsoft.Office.Interop.PowerPoint.Slides
            Dim oSlide As Microsoft.Office.Interop.PowerPoint.Slide
            Dim oShape As Microsoft.Office.Interop.PowerPoint.Shape
            Dim oShapes As Microsoft.Office.Interop.PowerPoint.Shapes
            Dim oTextFrame As Microsoft.Office.Interop.PowerPoint.TextFrame
            Dim oTextRange As Microsoft.Office.Interop.PowerPoint.TextRange

            pb.Value += 1
            pb1.Minimum = 0
            pb1.Value = 0
            pb1.Step = 1

            Try
                oApp = New Microsoft.Office.Interop.PowerPoint.Application

                oPreses = oApp.Presentations
                oPres = oPreses.Open(itm & ".ppt", , , False)
                oSlides = oPres.Slides
            Catch ex As Exception
              'error goes here
            End Try

           'loop through slides goes here
            For Each oSlide In oSlides
                oShapes = oSlide.Shapes
               
                'resultarray is fetched
                Dim PageArr() = RetrieveResult(itm & ".txt", Delimiter & oSlide.SlideIndex, "_")
                Dim AIFound As Boolean = False

               'shapeloop goes here...
                For Each oShape In oShapes
                    If InStr(oShape.Name, " ") = 0 Then
                        Dim ShapeArr() = RetrieveShapeResult(PageArr, oShape.Name)

                                    'graph is updated here
                                    oOLEFORMAT = oShape.OLEFormat
                                    oChart = oOLEFORMAT.Object
                                    For Each aItm As String In ShapeArr
                                        Dim tmparr = Split(aItm, Delimiter)
                                        Dim crd = Split(tmparr(1), "_")
                                        Try
                                            oChart.Application.DataSheet.Cells(crd(0), crd(1)).Value = tmparr(2)
                                        Catch ex As Exception
                                        End Try
                                    Next
                                    oChart.Application.Update()
                                    oChart.Application.Quit()


...and after these loops I release all objects through:

        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
        Catch
        Finally
            o = Nothing
        End Try


Is it OK to create an instance of PowerPoint within the loop? Should I declare PPT with

oPPT = CreateObject("Powerpoint.application")

instead?

Thanks,
Daniel
sveshiAsked:
Who is Participating?
 
david_barkerCommented:
I would try releasing each object separately, ie
  oApp.Close  'or whatever
  Set oApp=Nothing
Try releasing objects as soon as you have finished with them (if you don't need them anymore, like an oPres you have created and finished with), or keep them open (if you need them all the time, like with oApp). I guess your problems are related with that.

The following
  oPPT = CreateObject("Powerpoint.application")
shouldn't help. It's just another way of creating the instance (late binding vs. early binding).
Stick with your method of declaring types objects, I don't think it's related with the problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.