I need to copy some pictures from Excel (the pictures are in different folders within the same file) to PowerPoint, adding a slide for every picture in an already existing PowerPoint file (the file name is TBD_ppt_WRK which has already 2 slides inside that must be kept). I need to run the macro from Excel. The macro I’m using is:
Sub TBD_GENERATE_PPW()
' OPEN FILE TBD_ppt_WRK WHERE COPY THE PICTURES FROM EXCEL
Dim pp As Object
Set pp = CreateObject("PowerPoint.Application")
pp.Visible = True
pp.Presentations.Open ("C:\ Desktop\ ppt_TBD_WRK.ppt")
‘C:\ Desktop\ ppt_TBD_WRK.ppt is path & name of the PPW file where to add the slides
'Step 1: Declare variables
Dim ppPres As Object
Dim ppSlide As Object
Dim xlwksht As Worksheet
Dim MyRange As String
'Step 2:
Set pp = New PowerPoint.Application
Set ppPres = pp.ActivePresentation
pp.Visible = True
'Step 3: Set the ranges for your data and title
MyRange = "B2:S40"
'Step 4: Start the loop through each worksheet
For Each xlwksht In ActiveWorkbook.Worksheets
xlwksht.Select
Application.Wait (Now + TimeValue("0:00:1"))
'Step 5: Copy the range as picture
xlwksht.Range(MyRange).CopyPicture _
Appearance:=xlScreen, Format:=xlPicture
'Step 6: Count slides and add new blank slide as next available slide number
'(the number 12 represents the enumeration for a Blank Slide)
Slidecount = ppPres.Slides.Count
Set ppSlide = ppPres.Slides.Add(Slidecount + 1, 12)
ppSlide.Select
'Step 7: Paste the picture and adjust its position
ppSlide.Shapes.Paste.Select ‘ => THIS IS WHERE MACRO GOES IN ERROR SEE BELOW
pp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
pp.ActiveWindow.Selection.ShapeRange.Top = 75
pp.ActiveWindow.Selection.ShapeRange.Left = 125
pp.ActiveWindow.Selection.ShapeRange.Width = 550
'Step 8: Add the title to the slide then move to next worksheet
Next xlwksht
'Step 9: Saves the Powerpoint file updated with the name TDB pat_PAG_.ppt and closes it the Powerpoint file updated
ppPres.SaveAs "C:\Users\392710\Desktop\MENSILI_WRK\TDB patrimoni_PAG_.ppt", 1
ppPres.Close
'Step 10: Memory Cleanup
pp.Activate
Set ppSlide = Nothing
Set ppPres = Nothing
Set pp = Nothing
END SUB
The strange thing is that the macro works perfectly when I run it in debug (F8) step by step, but it doesn’t work anymore when I run it directly from the Excel Macro (Macro/View/Run). It stops after the first picture has been copied in the first slide added to ppw (see the comment in the code).
The error message is:
Run time error ‘-2147188160 (80048240)’ ShapeRange.Select : Invalid request. To select a shape, its view must be active.
Can you help me please?