问题描述
我需要将多个图表从Excel粘贴到PowerPoint.我发现了一些出色的VBA代码(主要在Jon Peltier的站点上).现在我的Powerpoint模板具有许多布局(例如,一张图表占据了大多数幻灯片,一张图表占据了一张幻灯片,等等).
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).
理想情况下,我希望能够选择布局(从15个中选择)并在所选布局中选择一个占位符(通常,我有一个标题,页脚,然后从1到4个占位符用于图表,图像,文本或以上所有).
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).
我不是VBA程序员,我只是使用一点逻辑并获取网上共享的代码.我不知道如何识别正确的布局(它们有名称,但这是变量吗?)或布局内的正确占位符(这里我什至都不知道如何识别它们).
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).
任何帮助,不胜感激.DF
Any help much appreciated.DF
在下面,我到处复制代码(主要是Jon Peltier的站点).
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
推荐答案
如果我了解您的问题,那么我认为这就是您想要的.
If I understood your problem then I think this is what you want.
您当前正在粘贴图表"打开" Slide 1
.您必须将其与Place Holder
相关的Place Holder
粘贴到Slide 1
中.
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.
快照
HTH
这篇关于将图表从Excel粘贴到特定布局中的特定占位符. Powepoint 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!