Search code examples
vbapowerpoint

Copy plot area not working properly in Powerpoint VBA


I've pieced together the following code to copy the format of a chart from one chart to another. The code is still messy as I'm trying to come up with the best way of referencing the charts (any ideas welcome!).

I'm having trouble making an exact copy of the format and positions mainly of the plot area and secondary access.

You must have two charts on a page, select the source first and then the destination. For now, they should be the same type.

Thanks for your help.

Sub CopyChartFormat()

' Define the source and destination charts
Dim sourceChart As Chart
Dim destChart As Chart

'Check if two charts are selected
If ActiveWindow.Selection.Type <> ppSelectionShapes Then
    MsgBox "Please select two charts."
    Exit Sub
End If

If ActiveWindow.Selection.ShapeRange.Count <> 2 Then
    MsgBox "Please select two charts."
    Exit Sub
End If

Set sourceChart = ActiveWindow.Selection.ShapeRange(1).Chart
Set destChart = ActiveWindow.Selection.ShapeRange(2).Chart
    
''    If .HasChart = True Then
''    End If

    '' Chart size
    ActiveWindow.Selection.ShapeRange(2).Width = ActiveWindow.Selection.ShapeRange(1).Width
    ActiveWindow.Selection.ShapeRange(2).Height = ActiveWindow.Selection.ShapeRange(1).Height
    
'' Adjust plot area size and fill

    With destChart.PlotArea
        .Top = sourceChart.PlotArea.Top
        .Left = sourceChart.PlotArea.Left
        .Height = sourceChart.PlotArea.Height
        .Width = sourceChart.PlotArea.Width
    
''        .Format.Fill.ForeColor.RGB = sourceChart.PlotArea.Format.Fill.ForeColor.RGB
''        .Format.Line.ForeColor.RGB = sourceChart.PlotArea.Format.Line.ForeColor.RGB
    
    
    End With


End Sub

Solution

  • You were missing the inside of the plot area. I added as many things as I could imagine. I tested and, if the font size is different, I had to run it twice to get the wanted results.

    
    Sub CopyChartFormat()
    
    ' Define the source and destination charts
    Dim sourceChart As Chart
    Dim destChart As Chart
    
    'Check if two charts are selected
    If ActiveWindow.Selection.Type <> ppSelectionShapes Then
        MsgBox "Please select two charts."
        Exit Sub
    End If
    
    If ActiveWindow.Selection.ShapeRange.Count <> 2 Then
        MsgBox "Please select two charts."
        Exit Sub
    End If
    
    Set sourceChart = ActiveWindow.Selection.ShapeRange(1).Chart
    Set destChart = ActiveWindow.Selection.ShapeRange(2).Chart
        
    ''    If .HasChart = True Then
    ''    End If
    
        '' Chart size
        ActiveWindow.Selection.ShapeRange(2).Width = ActiveWindow.Selection.ShapeRange(1).Width
        ActiveWindow.Selection.ShapeRange(2).Height = ActiveWindow.Selection.ShapeRange(1).Height
        
    '' Adjust plot area size and fill
    
        With destChart.PlotArea
            .Top = sourceChart.PlotArea.Top
            .Left = sourceChart.PlotArea.Left
            .Height = sourceChart.PlotArea.Height
            .Width = sourceChart.PlotArea.Width
            
            
            .InsideWidth = sourceChart.PlotArea.InsideWidth
            .InsideHeight = sourceChart.PlotArea.InsideHeight
            .InsideLeft = sourceChart.PlotArea.InsideLeft
            .InsideTop = sourceChart.PlotArea.InsideTop
    
            End With
            
            With destChart.Format
            .TextFrame2.TextRange.Font.Size = sourceChart.Format.TextFrame2.TextRange.Font.Size
            .TextFrame2.TextRange.Font.Name = sourceChart.Format.TextFrame2.TextRange.Font.Name
            End With
            
            
    
    ''        .Format.Fill.ForeColor.RGB = sourceChart.PlotArea.Format.Fill.ForeColor.RGB
    ''        .Format.Line.ForeColor.RGB = sourceChart.PlotArea.Format.Line.ForeColor.RGB
        
        
    '    End With
        
        With destChart.ChartTitle.Format
                .TextFrame2.TextRange.Font.Size = sourceChart.ChartTitle.Format.TextFrame2.TextRange.Font.Size
        End With
        
        With destChart.ChartArea.Format
        
            .TextFrame2.TextRange.Font.Size = sourceChart.ChartArea.Format.TextFrame2.TextRange.Font.Size
            
        End With
        
        
        With destChart.Legend
            .Position = sourceChart.Legend.Position
            .Left = sourceChart.Legend.Left
            .Top = sourceChart.Legend.Top
            .Width = sourceChart.Legend.Width
            .Height = sourceChart.Legend.Height
        End With
    
    
    'With destChart.Floor   'commented out as i keep on getting "Method failed"
    '
    '.Format.TextFrame2.TextRange.Font.Size = sourceChart.Floor.Format.TextFrame2.TextRange.Font.Size
    '
    'End With
    
    With destChart.Format
    .TextFrame2.TextRange.Font.Size = sourceChart.Format.TextFrame2.TextRange.Font.Size
    
    End With
    
    
    End Sub