The code below throws a runtime error on 3 out of 4 runs. I have an large Excel file an I'm trying to copy tables and pictures to a PowerPoint template. The tables are named "WDM_Tab_i" (i for numbers from 1 to 100) and pictures "WDM_Grafik_i_ x"(i same as table and x because some tables have two pictures). I first tried the code with a quite small number of tables and pictures (5 tables and 7 pictures or so). There the code worked perfectly fine. As I started adding tables and pictures, I quite often got runtime errors.
On Error Resume Next
For i = 1 To 20
On Error Resume Next
holder = Range("WDM_Tab_" & i).Rows.Count
If Err = 1004 Then
On Error GoTo 0
Exit For
Else
On Error GoTo 0
Set pptSlide = Pres.Slides.AddSlide(18 + i, pptLayout)
Pres.Slides(18 + i).Shapes("Titel 1").TextFrame.TextRange.Characters.Text = Range("WDM_Titl_" & i).Value
Range("WDM_Tab_" & i).Copy
Pres.Slides(18 + i).Shapes.PasteSpecial DataType:=2
Set myShape = Pres.Slides(18 + i).Shapes(Pres.Slides(18 + i).Shapes.Count)
myShape.Left = 25
myShape.Top = 100
myShape.Width = 450
x = 1
For Each sh In Sheets("Weiterdrehmomente").Shapes
If sh.Name = "WDM_Grafik_" & i & "_" & x Then
sh.Copy
Pres.Slides(18 + i).Shapes.PasteSpecial DataType:=2
Set myShape = Pres.Slides(18 + i).Shapes(Pres.Slides(18 + i).Shapes.Count)
myShape.Left = 480
myShape.Top = 100 + ((x - 1) * 150)
If myShape.Width <= 150 Then
myShape.Width = 150
ElseIf myShape.Width >= 455 Then
myShape.Width = 455
End If
x = x + 1
End If
Next sh
End If
Next i
It's This line
Pres.Slides(18 + i).Shapes.PasteSpecial DataType:=2
which gives me the error. When I added "On Error Resume Next" to the whole loop, the output was always different. Sometimes table 5 was missing sometimes picture 7 and in rare occasions, every table and every picture showed in the PP. Can you maybe tell me why it is so inconsistent? Thanks for your help! I am quite confused on how to fix it.