I am trying to copy a range of cells from Excel and paste it onto a slide of a PowerPoint presentation (both version 2016) with the original formatting.
I tried
Allg.Copy
mySlide2.Shapes.PasteSpecial DataType:=0
myPresentation.Slides(2).Shapes(3).Name = "AllgShape"
and it works most of the time, but not always. The following runtime error happens from time to time:
'-2147188160 (80048240)': Shapes.PasteSpecial: Invalid request. Clipboard is empty or conains data which may not be pasted here
because (I think) the clipboard is not populated in time. Therefore I tried this to just repeat the copying and pasting process if an error happens:
ALLGCOPY:
Allg.Copy
On Error GoTo ALLGCOPY:
mySlide2.Shapes.PasteSpecial DataType:=0
myPresentation.Slides(2).Shapes(3).Name = "AllgShape"
It seems that the error handler does not exactly what I think it does because sometimes it just pastes the same shape 2 times when running this code.
Then I tried
Allg.Copy
PowerPointApp.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting"
myPresentation.Slides(2).Shapes(3).Name = "AllgShape"
but then sometimes there's an issue with the assigning of the name for the shape, because it does not paste fast enough.
so I added a timer after the pasting
Public Sub Warten(ByVal MilliSekunden As Double)
Dim i As Double
Dim ENDE As Double
ENDE = Timer + (MilliSekunden / 1000)
Do While i < ENDE
DoEvents
i = Timer
Loop
End Sub
But this is unreliable because sometimes 100ms are sufficient, but sometimes even 2000ms aren't sufficient and I want the macro to run on most (also older) machines.
Preferably I want to work with the error handler and not with the timer as it is unreliable and depending on CPU usage.
Can someone tell me why the code with the error handler does not work and is sometimes pasting the same shape 2 times?
Thank you