How to add new slide while copying excel tables using “for” loop from Excel vba?

时光毁灭记忆、已成空白 提交于 2020-04-18 01:21:12

问题


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:

  1. You already Set myPresentation = PowerPointApp.ActivePresentation previously. I think you meant mySlide.
  2. PowerPointApp.ActivePresentation - use myPresentation, this is redundant.
  3. The method is Slides.AddSlide, not Application.AddSlide or Presentation.AddSlide.
  4. ...but you want Slides.Add since AddSlide takes a CustomLayout as its second parameter.
  5. PowerPointApp.Slides.Count - there's no Slides property of the application; that should be myPresentation.Slides.Count.
  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!