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

use*_*484 3 charts powerpoint vba placeholder excel-vba

我需要将多个图表从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
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 6

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

您当前正在粘贴图表" 打开 " Slide 1.你必须将它粘贴 "相关" Place HolderSlide 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
Run Code Online (Sandbox Code Playgroud)

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

快照

在此输入图像描述 HTH