Solved

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

Posted on 2004-10-21
320 Views
Last Modified: 2008-02-01
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
0
Question by:sveshi
    1 Comment
     
    LVL 6

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now