1
votes

I have found, corrected and tested to pieces of code for dealing with charts in Powerpoint.

The first piece is make the plots the same size and position them correctly (assuming there are two plots on the slide) and goes like this:

Sub ProcessAllSlides()

Dim sld As Slide
Dim Shp As Shape

For Each sld In ActivePresentation.Slides
    Call SetChartSizeAndPosition(sld.Shapes(1), 30, 120, 320, 240)
    Call SetChartSizeAndPosition(sld.Shapes(2), 370, 120, 320, 240)
Next

End Sub

Sub SetChartSizeAndPosition(Shp As Shape, Left As Single, Top As Single, Width As Single, Height As Single)

With Shp
    .Left = Left
    .Top = Top
    .Width = Width
    .Height = Height
End With

End Sub

This works on all slides in the presentation. I would like it to only function on the active slide.

The second piece is supposed to format the plot area (and not the chart area). See below:

Sub SizePlotArea()

Dim oSld As Slide
Dim oCht As Chart

Set oCht = ActiveWindow.Selection.ShapeRange(1).Chart

With oCht.PlotArea
    ' Edit these values as needed
    ' Change the following lines to e.g. Msgbox .Left etc
    ' to get the values of the chart you want to match others TO
    .Left = 0
    .Top = 0
    .Height = 220
    .Width = 300
End With

End Sub

Idealy I would like to combine the two such that they are done for all (two) charts on the active slide.

Anyone with any tips?

1

1 Answers

1
votes

Try the Merged Sub below, explanation inside the code's comments.

Option Explicit

Sub ProcessAllSlides()

Dim sld As Slide
Dim Shp As Shape
Dim oCht As Chart
Dim i As Long
Dim ChartIndex As Long

' set the Active Slide
Set sld = Application.ActiveWindow.View.Slide

ChartIndex = 1

' --- loop through the Slide shapes and search for the Shape of type chart
For i = 1 To sld.Shapes.Count
    If sld.Shapes(i).HasChart = msoTrue Then  ' if current shape is a chart
        Set Shp = sld.Shapes(i)
        Set oCht = Shp.Chart

        If ChartIndex = 1 Then ' first chart
            SetChartSizeAndPosition Shp, 30, 120, 320, 240
            ChartIndex = ChartIndex + 1

        ElseIf ChartIndex = 2 Then ' second chart
            SetChartSizeAndPosition Shp, 370, 120, 320, 240
        End If

        With oCht.PlotArea
            ' Edit these values as needed
            ' Change the following lines to e.g. Msgbox .Left etc
            ' to get the values of the chart you want to match others TO
            .Left = 0
            .Top = 0
            .Height = 220
            .Width = 300
        End With

        Set oCht = Nothing
        Set Shp = Nothing
    End If
Next i

End Sub