如何在使用 Excel vba 中的 "for" 循环复制 excel 表格时添加新幻灯片?
How to add new slide while copying excel tables using "for" loop from Excel vba?
Dim myPresentation As Object
Dim mySlide As Object
Dim PowerPointApp As Object
Dim shp As Object
Dim MySlideArray As Variant
Dim MyRangeArray As Variant
Dim x As Long
PowerPointApp.ActiveWindow.Panes(1).Activate
Set myPresentation = PowerPointApp.ActivePresentation
MySlideArray = Array(1, 2)
MyRangeArray = Array(Worksheets("name").Range("A3:E17"), Worksheets("age").Range("A22:E37"))
For x = LBound(MySlideArray) To UBound(MySlideArray)
MyRangeArray(x).Copy
Set shp = myPresentation.Slides(MySlideArray(x)).Shapes.PasteSpecial(DataType:=2)
Set myPresentation = PowerPointApp.ActivePresentation.AddSlide(PowerPointApp.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutBlank).Select
Next x
问题1)错误是"Object doesn't support this prop or method"就在Count+1.select
行。我的错误是什么?
问题 2) 如果我在 same sheet 中有两个单元格范围 "A1:E9" 和 "A11:E20" 我想粘贴到 same 幻灯片,有没有办法编写代码从 A1 中查找非空单元格并将数据复制到最后填充的行并粘贴到 powerpoint 中?
很抱歉问了这么长的问题。很高兴得到任何答案。
Set myPresentation = PowerPointApp.ActivePresentation.AddSlide(PowerPointApp.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutBlank).Select
这有几个问题:
- 您之前已经
Set myPresentation = PowerPointApp.ActivePresentation
。我想你的意思是 mySlide
.
PowerPointApp.ActivePresentation
- 使用myPresentation
,这是多余的。
- 方法是
Slides.AddSlide
,不是Application.AddSlide
或Presentation.AddSlide
。
- ...但是您需要
Slides.Add
,因为 AddSlide
将 CustomLayout
作为其第二个参数。
PowerPointApp.Slides.Count
- 没有Slides
属性的应用;那应该是 myPresentation.Slides.Count
.
PowerPoint.PpSlideLayout.ppLayoutBlank
- 请注意,此 early-binding,而其余代码使用 late-binding。 (Dim myPresentation As Object
、Dim mySlide As Object
等)。最好保持一致。我已经制定了早期绑定方法
经过这些修订:
Const ppLayoutBlank as Long = 12
With myPresentation.Slides
Set mySlide = .Add(.Count + 1, ppLayoutBlank)
End With
Dim myPresentation As Object
Dim mySlide As Object
Dim PowerPointApp As Object
Dim shp As Object
Dim MySlideArray As Variant
Dim MyRangeArray As Variant
Dim x As Long
PowerPointApp.ActiveWindow.Panes(1).Activate
Set myPresentation = PowerPointApp.ActivePresentation
MySlideArray = Array(1, 2)
MyRangeArray = Array(Worksheets("name").Range("A3:E17"), Worksheets("age").Range("A22:E37"))
For x = LBound(MySlideArray) To UBound(MySlideArray)
MyRangeArray(x).Copy
Set shp = myPresentation.Slides(MySlideArray(x)).Shapes.PasteSpecial(DataType:=2)
Set myPresentation = PowerPointApp.ActivePresentation.AddSlide(PowerPointApp.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutBlank).Select
Next x
问题1)错误是"Object doesn't support this prop or method"就在Count+1.select
行。我的错误是什么?
问题 2) 如果我在 same sheet 中有两个单元格范围 "A1:E9" 和 "A11:E20" 我想粘贴到 same 幻灯片,有没有办法编写代码从 A1 中查找非空单元格并将数据复制到最后填充的行并粘贴到 powerpoint 中?
很抱歉问了这么长的问题。很高兴得到任何答案。
Set myPresentation = PowerPointApp.ActivePresentation.AddSlide(PowerPointApp.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutBlank).Select
这有几个问题:
- 您之前已经
Set myPresentation = PowerPointApp.ActivePresentation
。我想你的意思是mySlide
. PowerPointApp.ActivePresentation
- 使用myPresentation
,这是多余的。- 方法是
Slides.AddSlide
,不是Application.AddSlide
或Presentation.AddSlide
。 - ...但是您需要
Slides.Add
,因为AddSlide
将CustomLayout
作为其第二个参数。 PowerPointApp.Slides.Count
- 没有Slides
属性的应用;那应该是myPresentation.Slides.Count
.PowerPoint.PpSlideLayout.ppLayoutBlank
- 请注意,此 early-binding,而其余代码使用 late-binding。 (Dim myPresentation As Object
、Dim mySlide As Object
等)。最好保持一致。我已经制定了早期绑定方法
经过这些修订:
Const ppLayoutBlank as Long = 12
With myPresentation.Slides
Set mySlide = .Add(.Count + 1, ppLayoutBlank)
End With