我是使用VBA的新手,所以以前有可能这样做,但是我找不到关于此的讨论。我已经创建了一个模板ppt,我需要将表格添加到特定的幻灯片中(参见图片)。
创建表并将其运行到Powerpoint中会更容易吗?或将其与可以填充的PPT中的现有表连接?
答案 0 :(得分:0)
您有两种选择。您可以将 Excel范围导出为OLE对象,PowerPoint表对象或链接的OLE对象。每种方法都有其优点和缺点,但是,如果您的格式不必完全匹配,我就将其粘贴为表格。
这里有一些代码将演示如何使用VBA从Excel将一系列单元格粘贴到PowerPoint。
Sub ExportMultipleRangeToPowerPoint_Method2()
'Declare PowerPoint Variables
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim SldArray as Variant
'Declare Excel Variables
Dim ExcRng As Range
Dim RngArray As Variant
'Populate our array. This will define two ranges that exist in Sheet 1.
'This also populates our slide array which specifies which slide each range is pasted on.
'YOU WILL NEED TO CHANGE THIS IN ORDER FOR IT TO WORK ON YOURS!
RngArray = Array(Sheet1.Range("A1:F8"), Sheet1.Range("A10:F18"))
SldArray = Array(1, 2)
'Create a new instance of PowerPoint
Set PPTApp = New PowerPoint.Application
PPTApp.Visible = True
'Create a new Presentation
Set PPTPres = PPTApp.Presentations.Add
'Loop through the range array, create a slide for each range, and copy that range on to the slide.
For x = LBound(RngArray) To UBound(RngArray)
'Set a reference to the range
Set ExcRng = RngArray(x)
'Copy Range
ExcRng.Copy
'Enable this line of code if you receive an error about the range not being in the clipboard - This will fix that error by pausing the program for ONE Second.
'Application.Wait Now + #12:00:01 AM#
'GRAB AN EXISTING SLIDE.
Set PPTSlide = PPTPres.Slide(SldArray(x))
'Paste the range in the slide as a Table (Default).
PPTSlide.Shapes.PasteSpecial DataType:=ppPasteDefault
Next x
End Sub
再次,这实际上取决于您对其进行更新的频率,但是在大多数情况下,我会促使人们在使其变得更难之前使其保持更容易。如果您需要进一步的逐步介绍,我确实在YouTube上提供了一系列文章,您可以继续阅读。如果您想做更多高级的事情,例如定位甚至粘贴为不同的数据类型,这将特别有用。这是播放列表:Excel to PowerPoint