1

At the moment, I'm just trying to follow the instructions here but I've had this issue before. For some reason, I'm unable to assign PowerPoint charts to a declared Chart variable. Using .HasChart, I've confirmed that there is definitely a chart there, and using Shapes(i).Type, I've confirmed that the Shape is of a Chart Type, but I get Run-time error '13': Type mismatch every time. I've tried this in multiple files and multiple chart types, but to no avail - I must be missing something obvious and silly, but I can't figure it out.

For example:

Dim plot As Chart

For Each shp In PPT.Slides(5).Shapes

    If shp.HasChart Then

        MsgBox (shp.Type)
        Set plot = shp.Chart 

    End If

Next shp

Any thoughts?

Community
  • 1
  • 1
tal
  • 13
  • 3

1 Answers1

4

Because a PowerPoint.Chart is not the same as an Excel.Chart (see note below...). Try:

Dim plot As PowerPoint.Chart

When you do Dim plot as Chart, because Chart is a member of the Excel object model, VBA will default to use Excel.Chart unless otherwise specified.

The above assumes early binding with a reference to the PowerPoint library. If you get a compile error like "User-defined Type not Defined", this means you haven't set a project reference to PowerPoint. Alternatively, you can Dim plot as Object (generic object type) but you lose the intellisense that way.

NOTE: Functionally speaking, a PowerPoint.Chart is essentially identical to an Excel.Chart or a Word.Chart, etc., they will have the same methods, properties, etc., and with few exceptions (none that I can think of readily) anything you can do to one, you can do to the other. The only issue is that when you're referring to a chart that resides within PowerPoint, strictly speaking it is not an Excel.Chart and it needs to be qualified as PowerPoint.Chart (or generic Object type) to avoid the mismatch error.

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • Thank you! When I declare variables, the autofill gives me the option to select one of two Chart variables so I thought this was taken care of, but I guess not. Thanks for resolving a long unanswered question! – tal Jan 30 '17 at 22:26
  • @tal yep, the two different `Chart`s are Excel and PowerPoint. Probably the Excel one will appear first in the list, but I'm not 100% sure about that :) – David Zemens Jan 30 '17 at 22:28
  • 1
    i've tried both selections from the drop-own but to no avail - I guess even with those there is still some ambiguity. – tal Jan 30 '17 at 22:29
  • @tal that actually sounds about right, now that you mention it. I know I've had trouble with the `Series` and other object types that are shared across different MS Office applications. – David Zemens Jan 30 '17 at 22:36
  • 1
    Damn @DavidZemens how do you know so much about Power Point Charts? – Michael David Watson Jan 31 '17 at 16:29
  • @MichaelDavidWatson hahahahah – David Zemens Jan 31 '17 at 16:33