Tuesday, June 28, 2011

Copy Chart from Excel and paste it in PowerPoint Presentation


If you want to copy a chart from Excel and paste it in PowerPoint presentation using VBA . Try this macro -


Sub export_to_ppt()

'In tools Reference add Microsoft PowerPoint

Dim PPApp           As PowerPoint.Application
Dim PPPres          As PowerPoint.Presentation
Dim PPSlide         As PowerPoint.slide
Dim SlideCount      As Integer

    Set PPApp = New PowerPoint.Application
    PPApp.Visible = True
    'create new ppt
    Set PPPres = PPApp.Presentations.Add
    'count no of slides
    SlideCount = PPPres.Slides.Count
    PPPres.ApplyTemplate Filename:="C:\Program Files\Microsoft Office\Document Themes 12\Median.thmx" ' if you want to apply theme
       'set layout of slide
    Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutTitleOnly)
    'add header
    PPSlide.Shapes(1).TextFrame.TextRange.Text = Sheets(1).ChartObjects("Chart 1").Chart.ChartTitle.Text ' add chart title as header
    'format header
    With PPSlide.Shapes(1).TextFrame.TextRange.Characters
        .Font.Size = 30
        .Font.Name = "Arial"
        .Font.Color = vbWhite
    End With
    
    With PPSlide.Shapes(1)
        .Fill.BackColor.RGB = RGB(79, 129, 189)
        .Height = 50
        .TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignLeft ' left align the header text
    End With
   
    Sheets(1).ChartObjects("Chart 1").Chart.ChartArea.Copy ' copy chart
    PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex ' activate the slide no
    PPSlide.Shapes.Paste.Select ' paste chart
    'ALIGN THE chart
    PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
    PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
   
    
    Set PPSlide = Nothing
    Set PPPres = Nothing
    Set PPApp = Nothing
  
End Sub


No comments:

Post a Comment

Import data from SQL

Macro to import data from SQL using ADO connection string: Sub Import_data_from_SQL() ' Tools -> References -> Microsoft Active...