如何自动创建许多PowerPoint演示文稿,其中包含带有Excel表格中的变量的幻灯片?

时间:2019-08-07 13:53:10

标签: excel vba automation powerpoint

我正在尝试编写一个代码,该代码使用Excel表中的值自动创建PowerPoint演示文稿。 A2,A3,A4等都将包含一个名称,每个pptx文件中的幻灯片将是一个包含3个段落的测试,并且首先以这种方式寻址这些单元格中的名称:

Dear 'BA2',

[paragraph1 ]

[paragraph 2]

Yours faithfully,
...

其他单元格也是如此。

这怎么办?

我一直在努力学习这一点,虽然有一些例子,但是对我来说很难将它们全部组合起来。

Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dm ppSlide As PowerPoint.Slide

Set ppApp = New PowerPoint.Application

ppApp.Visible = True
ppApp.Activate

Set ppPres = ppApp.Presentations.Add 

Set ppSlide = ppPres.Slides.Add(1, ppLayoutBlank)

Set ppApp = New PowerPoint.Application

ppApp.Visible = True
ppApp.Activate

set ppSlide = ppPres.Slides.Add(2,pp LayoutBlank(
ppSlide.Select

Range("A1").CurrentRegion.Copy
ppSlide.Shapes.Paste

您很可能知道,此代码将复制并粘贴整个表。放入一张幻灯片...与我想要的完全不同,但是我需要知道的是如何使用for循环或类似的方式将这些单元格粘贴到单独的工作表中。

您能帮我吗?将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

转到主幻灯片视图并根据需要制作模板; (例如特定的文字方块和物件),然后由excel每次都为该模板制作一张新幻灯片,并使用单元格中的变量填充这些方块。如果您只是学习编程,那将比尝试通过代码添加文本框和所有内容更好。 for循环上的代码取决于您的演示文稿幻灯片结构,如果可以指定数据和格式,则很清楚。 将此代码放入Powerpoint VBA中,它将使您可以访问excel中的数据,使用数据打开excel表,然后在循环中放入适当的内容后运行它。

Public Sub test()
Dim excelSht As Worksheet
Dim excelApp As Excel.Application
Set excelApp = GetObject(, "excel.application")
Set excelSht = excelApp.ActiveSheet
Dim path As String
With Application.FileDialog(msoFileDialogFolderPicker)
    .Show
    path = .SelectedItems(1)
End With
'Set rng = excelApp.Selection ' use this if you want to select the data in advance
Set rng = excelSht.Range("A2:A140") ' use this if your data range is fixed
For Each cel In rng
    For Each shp In Application.Presentations(1).Slides(1).Shapes
        If shp.Name Like "Title 1" Then shp.TextFrame.TextRange.Text = "Dear, " & cel.Value ' this one choose the name of the shape of which one you want to change the text.
    Next
    Application.Presentations(1).SaveAs (path & "\" & cel.Value & ".pptx")
Next
End Sub