问题
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
Question 1) The error is "Object doesn't support this prop or method" just at the Count+1.select
line. What is my mistake?
Question 2) If i have two ranges of cells "A1:E9" and "A11:E20" in same sheet that i want to paste in same slide, is there a way to write code which looks for non-empty cells from A1 and copies data till the last filled row and paste in powerpoint?
Apologies for the long question. Will be happy to get any answer.
回答1:
Set myPresentation = PowerPointApp.ActivePresentation.AddSlide(PowerPointApp.Slides.Count + 1, PowerPoint.PpSlideLayout.ppLayoutBlank).Select
There's a couple things wrong with this:
- You already
Set myPresentation = PowerPointApp.ActivePresentation
previously. I think you meantmySlide
. PowerPointApp.ActivePresentation
- usemyPresentation
, this is redundant.- The method is Slides.AddSlide, not
Application.AddSlide
orPresentation.AddSlide
. - ...but you want
Slides.Add
sinceAddSlide
takes aCustomLayout
as its second parameter. PowerPointApp.Slides.Count
- there's noSlides
property of the application; that should bemyPresentation.Slides.Count
.PowerPoint.PpSlideLayout.ppLayoutBlank
- note that this early-binding, while the rest of your code uses late-binding. (Dim myPresentation As Object
,Dim mySlide As Object
, etc.). Best to be consistent. I've laid out an early-binding approach
With those revisions:
Const ppLayoutBlank as Long = 12
With myPresentation.Slides
Set mySlide = .Add(.Count + 1, ppLayoutBlank)
End With
来源:https://stackoverflow.com/questions/61017894/how-to-add-new-slide-while-copying-excel-tables-using-for-loop-from-excel-vba