Eric Alan Hill
Eric Alan Hill

Reputation: 175

VBA PowerPoint Copy/Paste Inconsistencies

I have a VBA script written that searches through one PowerPoint (PRS_data) for a title match, and then copies the slide to a new PowerPoint (PRS). The slides on the source are simply a title and an image. When some of the slides are copied over, the images are either not carried over or I get a box with a red X.

I am using PowerPoint 2007 SP3, so that red X hotfix isn't the problem.

I also tried adding DoEvents and a Sleep as well, with no success.

If I leave the destination window maximized on the user's screen it seems to work, but if it is minimized, I get sporadic blank slides. The results are inconsistent. Also, I can confirm the slides do get copied, because the destination is an empty presentation and it is filled with slides that just have the correct titles on them. Again inconsistent results though.

Anyone ran into this before??

Here is the code:

For Each Sld In PRS_data.Slides
    If Sld.SlideIndex > 1 Then          
        If (Sld.Shapes.Title.TextFrame.TextRange.Text Like "*" & iDiv & " Division*") Then
            PRS_data.Slides.Range(Sld.SlideIndex).Cut
            PRS.Windows(1).Activate
            PRS.Slides.Paste
            DoEvents
        End If
    End If
Next

Upvotes: 1

Views: 2336

Answers (1)

Chris Thornton
Chris Thornton

Reputation: 15817

You cannot perform a paste immediately after a cut or copy. Other programs on the system are reacting to the cut, and therefore the clipboard is not available yet. Years ago, you probably would have gotten "cannot open clipboard" errors from PowerPoint, but not it apparently just fails silently.

Note that if this is an end-user application, and they have other clipboard-aware apps running, or are using remote desktop, those users will hate your program. The clipboard is provided for the convenience of the user, not the developer.

That said, you can probably make this work by adding a 2-second sleep immediately after the CUT/COPY command.

Upvotes: 1

Related Questions