Share via

Sample of VBA code to format chart on each slide the same demensions

Anonymous
2016-06-15T22:47:05+00:00

Hello,

I'm trying to find samples of VBA code to create a macro in my PPT deck to size and position each chart on each slide in the same position.  Plus make sure that all the fonts are the same: Segoe UI

Any suggestions as to where to go to find the syntax and or samples?

Thank you

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

John Korchok 232.4K Reputation points Volunteer Moderator
2016-06-16T18:01:57+00:00

Thanks for the code.

Every box on a slide is a shape. This means that if you have a title on a slide with 2 charts, you have 3 shapes on the slide. So it's a good idea to test whether the shape has a chart is before applying the chart resizing:

  With ActiveWindow.Selection.ShapeRange

    If .HasChart Then

      Perform Action

    End If

  End With

You specced your variables as Doubles, but if you check the help for Width and the other parameters, PowerPoint uses a Single. If you specify the number type when declaring, it has to be the same type that the parameter actually uses.

You're depending on the Selection object. This can be a good thing for the first selected chart that is used as the position model for the others. But to apply that position automatically to all the other charts, the selection object won't work. Instead you need to loop through each slide in the presentation. On each slide, you loop through all shapes, checking for whether a shape has a chart. Then you only apply the positioning to shapes that meet the criteria:

Sub ResizeCharts()

  Dim w As Single

  Dim h As Single

  Dim l As Single

  Dim t As Single

  With ActiveWindow.Selection.ShapeRange(1)

    If .HasChart = True Then

      w = .Width

      h = .Height

      l = .Left

      t = .Top

    Else

      MsgBox "Please select a chart with the correct position and size"

    End If

  End With

  Dim objSlide As Slide, objShape As Shape

  For Each objSlide In ActivePresentation.Slides

    For Each objShape In objSlide.Shapes

      If objShape.HasChart Then

        With objShape

          .Width = w

          .Height = h

          .Left = l

          .Top = t

        End With

      End If

    Next objShape

  Next objSlide

End Sub

Please note that if there are ever 2 charts per slide, they will end up on top of one another using this code.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

John Korchok 232.4K Reputation points Volunteer Moderator
2016-06-17T00:05:09+00:00

There are 2 tricks to finding the syntax for the object that you want to work with. One is to use the VBA Editor's Autocomplete. After you type:

With ActiveWindows.Selection.ShapeRange(1)

then type a dot, the VBE will pop up a list of properties and methods that can be used. For what you're trying to do .Chart looks more likely. Once you choose that and type another dot, another list pops up, the one including PlotArea. Select that and type another dot and a list appears that includes Height, Width etc.:

ActiveWindow.Selection.ShapeRange(1).Chart.PlotArea.Height

The other technique that helps discover properties and methods is the Object Browser in the VBE. If you choose View>Object Browser, you'll see a list of Classes that includes Chart. Click on Chart to see all the properties and methods that apply to it. Select any of those, right-click and choose Help to get whatever detail is in the VBA help system. In general, VBA help has declined in quality over time, so it can be helpful to have older versions installed, like Office 2007 or 2010.

Setting a font would be simpler if you could choose .Chart.ChartArea.Font, but instead you have to drill down with every element with statements like .Chart.Legend.Format.TextFrame2.TextRange.Font. It might be easiest to control the font by setting the theme for the presentation. Then charts created in PowerPoint normally automatically use the theme font.

Was this answer helpful?

0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-06-16T16:21:15+00:00

    I need syntax that will capture the chart size, position - plot area size and position - text font position and size, of an existing chart in a powerpoint deck.  I have nine charts in one deck to adjust to the same parameters so each chart gives the perception of staying in the same position when the slides advance forward.

    I tried this syntax and received a run-time error Method 'ShapeRange' of object 'Chartobject'failed'

    Sub copysizeandposition()

    Dim w As Double

        Dim h As Double

        Dim l As Double

        Dim t As Double

        With ActiveWindow.Selection.ShapeRange(1)

            w = .Width

            h = .Height

            l = .Left

            t = .Top

        End With

        With ActiveWindow.Selection.ShapeRange(2)

            .Width = w

            .Height = h

            .Left = l

            .Top = t

        End With

    End Sub

    This is what I recorded in Excel to capture syntax.  I changed ActiveSheet to ActiveChart. 

    Sub test()

    ' Position pasted chart

    Activechart.ChartObjects("Chart 2").Activate

        Activechart.ChartObjects("Chart 2").Activate

        Activechart.Shapes("Chart 2").IncrementLeft -16.5

        Activechart.Shapes("Chart 2").IncrementTop -13.5

        Activechart.ChartObjects("Chart 2").Activate

        Activechart.Axes(xlValue).MajorGridlines.Select

        Activechart.PlotArea.Select

        Activechart.PlotArea.Select

        Selection.Width = 601.933

        Selection.Height = 383.343

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2016-06-16T14:58:08+00:00

    I have been binging and have found several examples that do not work. very frustrating.  I was wondering if there is a library like MSN has for SQL that provides the syntax.  I even recorded a macro in excel for charts and copied and pasted that syntax into powerpoint and it did not work.  So in essence I'm re-engineering steps to try and get my PPT VBA to work with no luck.  again very frustrating.

    Was this answer helpful?

    0 comments No comments
  3. John Korchok 232.4K Reputation points Volunteer Moderator
    2016-06-16T14:30:30+00:00

    Google is your friend. powerpoint vba chart position gets a page of hits on size and position of charts. powerpoint vba chart font shows a variety of chart font issues that can be solved by macros.

    Was this answer helpful?

    0 comments No comments