vba 以编程方式从 Excel 数据创建 [Mail Merge] PowerPoint 幻灯片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18114766/
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 22:37:18  来源:igfitidea点击:

Programmatically create [Mail Merge] PowerPoint slides from Excel data

excelvbapowerpointmailmerge

提问by Nikhil Goel

Using Powerpoint 2010 for Mac.

使用 Mac 版 Powerpoint 2010。

I have a 100-slide presentation that needs to be created weekly. Each slide has an identical template. The elements on each slide consist of 5 text fields and a picture, each slide corresponding to columns of data in an Excel table.

我有一个需要每周创建 100 张幻灯片的演示文稿。每张幻灯片都有一个相同的模板。每张幻灯片上的元素由 5 个文本字段和一张图片组成,每张幻灯片对应于 Excel 表格中的数据列。

Can anyone point me to a code example or detailed walkthrough of how I can loop through the rows of this Excel file and programmatically create these slides?

任何人都可以指出我如何循环遍历此 Excel 文件的行并以编程方式创建这些幻灯片的代码示例或详细演练吗?

回答by RowanC

This is definately possible, and quite fun :-)

这绝对是可能的,而且很有趣:-)

My first recommendation, is to create a powerpoint template using master views, with placeholders and titles all ready to go. Then run the following powerpoint macro, so that you can get the name and index/id for every shape on the page.

我的第一个建议是使用主视图创建一个 powerpoint 模板,占位符和标题都准备好了。然后运行以下 powerpoint 宏,以便您可以获得页面上每个形状的名称和索引/ID。

Sub nameshapes()

Dim sld As Slide
Dim shp As Shape
Set sld = Application.ActivePresentation.Slides(1)

For Each shp In sld.Shapes
    shp.TextEffect.Text = shp.name & " " & shp.ID
Next
End Sub

This is a quick and dirty piece of code that puts the index & name of each item on your template page, into the shape itself. Memorise, record these. These are your reference points for where you want stuff to go. I'm not going to go over the basics of looping etc, but go over the key code peices.

这是一段快速而肮脏的代码,它将模板页面上每个项目的索引和名称放入形状本身。记住,记录这些。这些是您想要的东西去哪里的参考点。我不打算讨论循环等的基础知识,而是讨论关键代码片段。

1) Open and gain control of powerpoint from excel.

1)从excel打开并获得对powerpoint的控制。

Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = True
Set myPPT = ppt.Presentations.add
myPPT.ApplyTemplate ("Your template here.potx")

I then add all the pages I'll need, (this can vary depending on your application, and the number of rows, and whether you do this at the start, or in the process, will depend on whether you have mapped which page you should put the data onto, in your data)

然后我添加我需要的所有页面(这可能因您的应用程序和行数而异,以及您是在开始时还是在过程中这样做,将取决于您是否映射了哪个页面应该将数据放在您的数据中)

For x = 1 To NumberOfPages
    myPPT.Slides.AddSlide x, myPPT.SlideMaster.CustomLayouts(2) '2 is the index of the template I wish to use (in master views, the index is the order they're in (starting from 1)
Next

I'm assuming that you know which page you want to update at each row, so:

我假设您知道要在每一行更新哪个页面,因此:

For Each dr In .rows  'I'm storing my data in a special collection, you will need to adapt this
    Set currSlide = myPPT.Slides(dr.cell("OutputPage").Value) 'go to the right page
    Sheets(dr.cell("SheetName").toString).Activate 'make sure the data you want is active
    ActiveSheet.Range(Names(dr.cell("ChartID").Value)).CopyPicture 'copy the table as a picture, this is easiest for formatting in powerpoint, but you can do lots of things here
    currSlide.Select
    currSlide.Shapes("Content Placeholder " & dr.cell("Output Position").toString).Select 'the output position is the index from the first bit of code,
    ppt.ActiveWindow.View.Paste
next

Now, your application will definitely vary from this but I hope all of the basic necessities are there, and this should be a good starting point.

现在,您的应用程序肯定会有所不同,但我希望所有基本必需品都在那里,这应该是一个很好的起点。

回答by André van Delft

The stand alone application SlideMightaccepts a PowerPoint template, a JSON data file and a configuration, and merges these into a presentation.

独立应用程序SlideMight接受一个 PowerPoint 模板、一个 JSON 数据文件和一个配置,并将这些合并到一个演示文稿中。

Iterations may be over slides and over table rows and they may be nested. Image substitution is supported, also in table cells.

迭代可能在幻灯片和表格行上进行,并且它们可能是嵌套的。支持图像替换,也在表格单元格中。

Disclaimer: I am developer & seller of SlideMight.

免责声明:我是 SlideMight 的开发商和卖家。