vba 将 Excel 中的图表粘贴到特定布局中的特定占位符。电源点 2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10282466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:53:58  来源:igfitidea点击:

Paste a chart from Excel to a specific placeholder in a specific layout. Powepoint 2010

vbachartsexcel-vbapowerpointplaceholder

提问by user1351484

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).

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

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).

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

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).

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

Any help much appreciated. DF

非常感谢任何帮助。DF

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

在下面,我在这里和那里复制的代码(主要是 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

回答by Siddharth Rout

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 Holderin Slide 1.

您当前正在粘贴图表“Slide 1。你必须粘贴“”有关Place HolderSlide 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

快照

enter image description hereHTH

在此处输入图片说明HTH