将图表从Excel粘贴到特定布局中的特定占位符。 Powerpoint 2010

时间:2012-04-23 14:24:47

标签: vba charts excel-vba powerpoint placeholder

我需要将多个图表从excel粘贴到powerpoint。 我发现了一些优秀的VBA代码(主要在Jon Peltier的网站上)。 现在我的powerpoint模板有许多布局(例如,1张图表占据了大部分幻灯片或1张图表,幻灯片中有一个文本框等)。

我想要的是图表成为幻灯片布局的一部分,这样如果我重新格式化幻灯片 - 例如我按照上面给出的示例更改布局 - 图表将相应移动。 目前我可以粘贴占位符所在的位置,具有正确的大小和所有内容,但它不在占位符中,它位于占位符上(因此如果我更改布局,它会保留在那里)。

理想情况下,我希望能够选择布局(从15开始)并在所选布局中选择占位符(通常我有一个标题,一个页脚,然后是1到4个占位符,用于图表,图像,文本或所有这些)。

我不是VBA程序员,我只是使用一点逻辑并抓取代码,这些代码在网上是很好的共享。我不知道如何识别正确的布局(它们有名称,但是变量是什么?),也不是布局中的正确占位符(这里我甚至不知道如何识别它们)。

任何帮助非常感谢。 DF

在下面,我在这里和那里复制的代码(主要是Jon Peltier的网站)。

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 个答案:

答案 0 :(得分:6)

如果我理解你的问题,那么我认为这就是你想要的。

您目前正在粘贴图表“开启Slide 1。您必须在Place Holder中将其粘贴为“ ”相关的Slide 1

修改您的代码以合并此内容(经过审核和测试

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

现在,即使您更改了布局,图表也会相应移动。

<强>快照

enter image description here HTH