Question Specified cast is not valid

lkritchey

Member
Joined
Sep 26, 2008
Messages
8
Location
Mechanicsburg, PA
Programming Experience
1-3
Hi! I have been programming a Metrics application for the past few months. For all intensive purposes, I have just completed the programming. However, I recently started to get the error "Specified cast is not valid". You can see the stack trace below:

Message: Specified cast is not valid.
StackTrace: at Microsoft.Office.Interop.PowerPoint.OLEFormat.get_Object() at Metrics_Application.Utilities.PrepareGraph(Presentation mainPres, String ObjectNum, Int32 slideDiff) in C:\RSNF Metrics\Metrics Application\Metrics Application\Utilities.vb:line 79 at Metrics_Application.PPTBuilder.IMSectionSingle(Presentation mainpres, Int32 intTempCount, String strIMSection) in C:\RSNF Metrics\Metrics Application\Metrics Application\PPTBuilder.vb:line 305 at Metrics_Application.PPTBuilder.BuildPPT() in C:\RSNF Metrics\Metrics Application\Metrics Application\PPTBuilder.vb:line 151

Date/Time: 9/24/2009 14:49:57

When I run the program, it crashes at various times - not always at the same point in the program. I have found that when it crashes, it always crashes at line 79 of the Utilities class (see code below):

VB.NET:
    Public Function PrepareGraph(ByVal mainPres As PowerPoint.Presentation, ByVal ObjectNum As String, ByVal slideDiff As Integer)

        Dim s1 As PowerPoint.Slide = mainPres.Slides.Item(mainPres.Slides.Count - slideDiff)
       [COLOR="Red"][B] Dim oGraph = s1.Shapes.Item(ObjectNum).OLEFormat.Object[/B][/COLOR]
        Dim Graph As Graph.Range = oGraph.Application.DataSheet.Cells
        Dim ds As Graph.DataSheet = oGraph.Application.DataSheet()
        ds.Cells.Clear()
        ds.Cells.ClearContents()
        Graph.Cells.Clear()
        ds.Range("A0:Z0").NumberFormat = "mmm dd"
        System.Windows.Forms.Application.DoEvents()
        Return Graph

    End Function

I am passing into the method the Powerpoint Presentation object, the string specifying the Object name of the graph Ole Shape, and an integer for the slideDiff (to help differentiate which slide the method is modifying). Also, for context, I am using this to automate a PPT presentation, using Powerpoint.Interop COM objects.

I have tried to research this on various forums. Typically, it seems like this problem is found when accessing databases, or when someone tries to put a string into an integer variable (or another type of incompatible variable type). Looking at the code, I always bring in the string for the Object Number...so the variable type is correct there.

Any help with identifying the problem and / or directing me to the solution is much appreciated! Thank you!
 
Back
Top