0
votes

I'm trying to resize a table I copied into PowerPoint from Excel, but I'm getting the following error when I try to set the active shape to resize.

Run-time error '-2147188160 (80048240)':

Method 'Item' of object 'Shapes' failed

The error highlights this part in the code:

Set shp = sld.Shapes(i)

The strange part is that after I hit 'Debug' and hit 'Run' again, the code will continue running normally through my loop until it hits that same line of code. I have 10 shapes already on the PowerPoint slide, so that's why I set i=11. Any idea how to make this error go away? Or a better way to resize tables than what I'm currently doing

Option Explicit
Sub PPT_Populator()

Dim ppt_app As New PowerPoint.Application
Dim pres As PowerPoint.Presentation
Dim sld As PowerPoint.slide
Dim shp As PowerPoint.shape
Dim wb As Workbook
Dim rng As Range

Dim vSheet$
Dim vRange$
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vSlide As Long
Dim i As Long

Set pres = ppt_app.Presentations.Open([pptPath])
Set wb = ThisWorkbook

i = 11

For Each rng In Range("Market_Objects_Table")
 With ThisWorkbook.Sheets("PPT Populator")
     vSheet$ = .Cells(rng.Row, 4).Value
     vRange$ = .Cells(rng.Row, 5).Value
     vWidth = .Cells(rng.Row, 6).Value
     vHeight = .Cells(rng.Row, 7).Value
     vTop = .Cells(rng.Row, 8).Value
     vLeft = .Cells(rng.Row, 9).Value
     vSlide = .Cells(rng.Row, 10).Value
 End With
ThisWorkbook.Sheets(vSheet$).Activate
ThisWorkbook.Sheets(vSheet$).Range(vRange$).Copy

Set sld = pres.Slides(vSlide)
pres.Slides(vSlide).Select
ppt_app.CommandBars.ExecuteMso ("PasteExcelTableDestinationTableStyle")

Set shp = sld.Shapes(i)       <----error happens here

With shp
    .LockAspectRatio = msoFalse
    .Top = vTop
    .Left = vLeft
    .Width = vWidth
    .Height = vHeight
End With


Set sld = Nothing
Set shp = Nothing
Application.CutCopyMode = False
i = i + 1
Next rng


End Sub
1
While your code executes the next statement immediately after a paste into PPT, sometimes the paste takes a bit longer. Try adding a doevents loop after the paste to give PPT some time to process the paste. - Steve Rindsberg

1 Answers

0
votes

I usually get this error when the shape index doesn't exist. Maybe instead of setting i=11 manually, you could just use the last shape, created by the paste command:

Set shp = sld.Shapes(sld.Shapes.Count)