【发布时间】:2018-12-24 10:05:36
【问题描述】:
我不熟悉将 excel 与其他应用程序链接,想知道是否有办法将我的电子表格复制并粘贴到 ppt 幻灯片中?唯一的事情是,我有一个包含数百行的电子表格。我正在寻找一种方法来循环遍历并粘贴电子表格,每张幻灯片 15 个,以及表格的标题。有什么办法吗?我脑海中的伪代码是这样的:
k=last row
for (i=0;i<k;i+15)
tbl.Row(i):tbl.Row(i+15) select
selection.copy into new ppt slide
这是我目前所拥有的:
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Dim i As Integer
i = 1
Do While i < 3
Set tbl = ActiveSheet.ListObjects("TableAll")
'Copy Range from Excel
Set Rng = tbl.Rows((i), (i + 4)).Range
'Create an Instance of PowerPoint
On Error Resume Next
'Is PowerPoint already opened?
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'Clear the error between errors
Err.Clear
'If PowerPoint is not already open then open PowerPoint
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
'Optimize Code
Application.ScreenUpdating = False
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Add
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
'Copy Excel Range
Rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
'Set position:
myShape.Left = 66
myShape.Top = 152
'Make PowerPoint Visible and Active
PowerPointApp.Visible = True
PowerPointApp.Activate
'Clear The Clipboard
Application.CutCopyMode = False
i = i + 1
Loop
提前致谢!
【问题讨论】:
-
作为建议 - 在您的
Do循环之外创建或获取 PowerPoint 实例。Set tbl...、Application.ScrenUpdating = False也是如此 - 即任何应该只做一次而不是重复的事情。 -
谢谢,我会把它排除在我的循环之外,但我怎样才能让它选择每 nth-n+10 行?
-
您希望将标题和每组 15 行作为一个图像粘贴吗?此外,使用您的伪代码 - 您想确保
i+15 < k,而不是i < k,对吗?另外,我看不到您当前代码中的k或其等效项的位置。 -
是的,我想要那个,每 15 行的标题。如果我可以将它粘贴为表格而不是图像,那就太酷了,但这不是一个大问题。你说得对,应该是 i+15
-
如果您希望它们作为一个图像,也许在您复制/粘贴时隐藏行? IE。复制标题和第 1-15 行,粘贴,然后隐藏第 1-15 行,复制标题和第 16-30 行等
标签: excel vba loops powerpoint