I am trying to copy a ChartObject created in a workbook sheet and paste it into a powerpoint 2010 slide, such that it becomes a standalone Excel Chart with data (embedded).
When i do it manually i.e :
- Copy ChartObject present in WorkSheet
- Goto Powerpoint Slide
- Click PasteSpecial & select "Use Destination Theme & Embed Workbook (H)".
- Right-click the ChartObject in Powerpoint & click "Edit Data".
Then i get a new standalone workbook with Title "Chart in Microsoft Excel", that shows the Chart as well as data. I have not been able to replicate this scenario where i get a new standalone workbook with Title "Chart in Microsoft Excel" using Excel VBA. Can someone help?
Assuming there is a simple column chart on the sheet, here is the code:
Option Explicit
Sub doit()
Dim Temp As Workbook
Dim Rng As Range
Dim ChtObj As ChartObject
With ThisWorkbook.Sheets(1)
Set ChtObj = .ChartObjects(1)
With ChtObj
.Copy
End With
Dim ppapp As Object
Dim pppres As Object
Dim ppslide As Object
On Error Resume Next
Set ppapp = GetObject(, "Powerpoint.Application")
If ppapp Is Nothing Then
Set ppapp = CreateObject("Powerpoint.Application")
End If
On Error GoTo 0
Set pppres = ppapp.presentations.Add
Set ppslide = pppres.Slides.Add(1, 12)
With ppapp
.Activate
.Visible = msoTrue
.ActiveWindow.viewtype = 1
End With
ChtObj.Copy
ppapp.ActiveWindow.View.Paste
End With
End Sub
.pastespecial
to get you to the point no. 3. Point no. 4 is not replicable- you can make workbook edition by VBA therefore you don't need to simulate right-click in such situation. – Kazimierz Jawor