2

I have a macro that gets a shape from one slide by its unique tag properties and then pastes it to another slide.

It works as expected on many different versions of OS (Win 7, 8, 8.1) and versions of PowerPoint (2007, 2010, 2013, 32 & 64 bit).

But, I just had one user where the paste command fails due to the clipboard being empty.

This is the code snippet (I haven't included the ShapeByTag) function which returns a shape, for simplicity reasons:

Dim oShp as ShapeRange 
ShapeByTag(TagName, TagValue).Copy
Set oShp = oSld.Shapes.Paste

When the error occurs on the second line "-2147188160 Shapes (unknown member) : Invalid request. Clipboard is empty or contains data which may not be pasted here." I can enter the VBE and rerun the command successfully, indicating to me that there is a timing issue.

But the user's machine is a brand new i5 Windows 8.1 Pro (64 bit) laptop with 8GBRAM which is more powerful that many other user machines where the issue does not occur. He's using PowerPoint 2013 32 bit.

I can overcome the problem by doing this:

Dim oShp as ShapeRange
ShapeByTag(TagName, TagValue).Copy
On Error Resume Next
RetryPaste:
Set oShp = oSld.Shapes.Paste
If Err <> 0 Then Err.Clear: GoTo RetryPaste
On Error Goto 0

But why is the error occurring in the first place? I would have expected that the .Copy method would be pretty instant and that the following line wouldn't execute until the underlying OS had completed the copy operation. I'm also going to add a Timer to see how long a delay is actually being injected.

Jamie Garroch - MVP
  • 2,839
  • 2
  • 16
  • 24

4 Answers4

0

Is oshp declared as a Shape??

Shapes.Paste returns a ShapeRange

Can't see how this would cause your particular error bur worth a check.

John Wilson
  • 319
  • 1
  • 3
  • I just came back to the post to add that very declaration and saw your comment John. Yes it is declared as ShapeRange (if declared as Shape, an infinite loop occurs in my workaround due to error 13). – Jamie Garroch - MVP May 07 '14 at 12:41
  • Never seen this but maybe make sure the slide to be pasted on is active and maybe a loop to check oshp now exists?? – John Wilson May 07 '14 at 13:16
0

DoEvents is worth a try:

ShapeByTag(TagName, TagValue).Copy
DoEvents
Set oShp = oSld.Shapes.Paste

DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue [...]

such as, maybe, populating the clipboard? Haven't tested this, but it seems plausible to me.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
  • Ooh, I forgot to try that Jean-François. Will try and report back. I am still perplexed as to how it can error on such a well specified brand new machine :-( – Jamie Garroch - MVP May 07 '14 at 12:40
  • Right, just tested the DoEvents on a remote session and it oddly doesn't fix it. Interestingly, I wrote a test macro to copy a shape from slide 1 to slide 2, 20 times and set a very basic VBA timer (not the WinAPI one) to get an idea of how long it's taking to make the retry attempts and it's anything between 0 and 0.2 seconds and every time I run the macro, the results are very different! My original fix doesn't work either, not until I move the RetryPaste label to before the Copy method. – Jamie Garroch - MVP May 07 '14 at 14:42
0

Never seen this but maybe make sure the slide you are pasting to is active and add a loop to check oshp has been created.

ActiveWindow.View.GotoSlide (osld.SlideIndex)
On Error Resume Next
Set oShp = Nothing
While oShp Is Nothing
Set oShp = osld.Shapes.Paste
Wend
John Wilson
  • 319
  • 1
  • 3
0

In addition to John's suggestion, you may want to ensure that a shape really is being copied:

Dim oShp as ShapeRange 
Dim oSh as Shape
Set oSh = ShapeByTag(TagName, TagValue)
If not oSh is nothing then
   ' You know that ShapeByTag returned a reference to a shape 
   ' Continue with your existing code:
   oSh.Copy
   ' etc
End if
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34