Search code examples
excelvbapowerpoint

Copying charts from Excel to PPT


I have an Excel with multiple sheets,each sheet having multiple charts. I want to copy a chart from a specific sheet of an Excel, to a particular slideno in PPT; with specific dimensions (ie height and width) and positions using VBA.

I am able to do the same.

However,when im trying to do so; other shapes in the ppt are also getting re-positioned to the same position, along with the charts.

Here is my code

wkbk.Sheets("Sheet2").Shapes("chart1").Copy
ActivePresentation.Slides(1).Shapes.Range.Height = embededpicrange.Cells(1, 3).Value
ActivePresentation.Slides(1).Shapes.Range.Width = embededpicrange.Cells(1, 4).Value

How can we change the position of charts alone with the above code.

Need some guidance on this


Solution

  • This may be help you:

    Sub copyChartToPP()
    
    'Declare the needed variables
    Dim newPP As PowerPoint.Application
    Dim currentSlide As PowerPoint.Slide
    Dim Xchart As Excel.ChartObject
    'Check if PowerPoint is activate:
    On Error Resume Next
    Set newPP = GetObject(, "PowerPoint.Application")
    On Error GoTo 0
    'Open PowerPoint if not activate
    If newPP Is Nothing Then
        Set newPP = New PowerPoint.Application
    End If
    'Create a new presentation in powerPoint
    If newPP.Presentations.Count = 0 Then
        newPP.Presentations.Add
    End If
    'Display the PowerPoint presentation
    newPowerPoint.Visible = True
    'Locate Excel charts to paste into the new PowerPoint presentation
    For Each Xchart In ActiveSheet.ChartObjects
    'Add a new slide in PowerPoint for each Excel chart
        newPP.ActivePresentation.Slides.Add newPP.ActivePresentation.Slides.Count + 1, 
    ppLayoutText
        newPP.ActiveWindow.View.GotoSlide newPP.ActivePresentation.Slides.Count
        Set currentSlide = 
    newPP.ActivePresentation.Slides(newPP.ActivePresentation.Slides.Count)
    'Copy each Excel chart and paste it into PowerPoint as an Metafile image
        Xchart.Select
    ActiveChart.ChartArea.Copy
        currentSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
    'Copy and paste chart title as the slide title in PowerPoint
        currentSlide.Shapes(1).TextFrame.TextRange.Text = cht.Chart.ChartTitle.Text
    'Adjust the slide position for each chart slide in PowerPoint. Note that you can 
    'adjust the values to position the chart on the slide to your liking
        newPP.ActiveWindow.Selection.ShapeRange.Left = 25
        newPP.ActiveWindow.Selection.ShapeRange.Top = 150
        currentSlide.Shapes(2).Width = 250
        currentSlide.Shapes(2).Left = 500
     Next
     End Sub