Paste a chart from Excel to a specific placeholder

2019-04-01 00:04发布

问题:

I need to paste multiple charts from excel to powerpoint. I found some excellent VBA code (mainly on Jon Peltier's site). Now my powerpoint template has a number of layouts (e.g. with 1 chart occupying most of the slides or 1 chart and one text box in a slide, etc etc).

What I want is the chart to become part of the slide layout so that if I reformat the slide - e.g. I change the layout as in the example given above - the chart will move accordingly. Currently I am able to paste in the spot where the placeholder is, with right size and everything, but it is not IN the placeholder, it's ON the placeholder (and therefore it stays there if I change layout).

Ideally I would like to be able to choose the layout (from 15) and choose the placeholder in the selected layout (typically I have a title, a footer, and then from 1 to 4 placeholders for charts, images, text or all of the above).

I am not a VBA programmer, I just use a little bit of logic and grab codes which is kindly shared on the net. I have not clue how to identify the proper layout (they have names, but is that the variable?) nor the proper placeholder within the layout (here I do not even know how to identify them).

Any help much appreciated. DF

In the following, the code I copied here and there (mainly Jon Peltier's site).

Sub ChartToPresentation()
' Set a VBE reference to Microsoft PowerPoint Object Library

Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim AddSlidesToEnd As Boolean

AddSlidesToEnd = True

' Make sure a chart is selected
If ActiveChart Is Nothing Then
  MsgBox "Please select a chart and try again.", vbExclamation, _
  "No Chart Selected"
Else
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
' Reference active slide
Set PPSlide = PPPres.Slides _
(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)

' Copy chart
ActiveChart.ChartArea.Copy

' Paste chart
PPSlide.Shapes.Paste.Select

' Position pasted chart
' This is the keypoint
' I want to replace this with the selection of appropriate layout 
' and placeholder in that layout
PPApp.ActiveWindow.Selection.ShapeRange.Left = 19.56
PPApp.ActiveWindow.Selection.ShapeRange.Top = 66.33
PPApp.ActiveWindow.Selection.ShapeRange.Width = 366.8
PPApp.ActiveWindow.Selection.ShapeRange.Height = 424.62


If PPApp.ActivePresentation.Slides.Count = 0 Then

' Other key point
' can I add a specific layout, for example one named Two Content Layout + takeout
 Set PPSlide = PPApp.ActivePresentation.Slides.Add(1, ppLayoutBlank)
Else
    If AddSlidesToEnd Then
         'Appends slides to end of presentation and makes last slide active
        PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
        PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count
        Set PPSlide = PPApp.ActivePresentation.Slides(PPApp.ActivePresentation.Slides.Count)
    Else
         'Sets current slide to active slide
        Set PPSlide = PPApp.ActiveWindow.View.Slide
    End If
End If


'Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing


End If

End Sub

回答1:

If I understood your problem then I think this is what you want.

You are currently pasting the chart "On" Slide 1. You have to paste it "In" relevant Place Holder in Slide 1.

Amend your code to incorporate this (TRIED AND TESTED)

Dim nPlcHolder As Long

With PPPres
    nPlcHolder = 2 '<~~ The place holder where you have to paste

    .Slides(1).Shapes.Placeholders(nPlcHolder).Select msoTrue
    .Windows(1).View.PasteSpecial (ppPasteMetafilePicture)
End With

Now even if you change your layout, the charts will move accordingly.

SNAPSHOT

HTH