简体   繁体   中英

What is the best way to paste excel ranges as pictures into ppt

I've got a macro that opens a ppt template file and populates slides with the contents of pivot table, pasting these as enhanced metadata - pretty straight forward job - filter pivot using a criteria, copy range, paste special with DataType:=2.

The issue is that I've got this process as a loop that does the job of opening ppt template, populating slides with these images and saving as a copy 10 times (producing 10 files) and at random I get this error:

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

Shapes.PasteSpecial: Invalid request. Clipboard is empty or contains data which may not be pasted here."

Now the problem with this error is that it's not true, because all I have to do is click RUN and the macro then proceeds with no issue without me changing a single thing. And it may run with no issues for a couple of times and then throw an error at random - the loop iteration, the pivot table range copied and the slide number are all inconsistent.

What I assume is the problem is the memory, hence my question - is there a better way to copy excel range and paste as image into ppt file?

My code at the moment is very simple:

ws.PivotTables("pivot").TableRange2.Copy
MySlide.Shapes.PasteSpecial DataType:=2

Maybe this method is inefficient? Will changing the DataType help? (I assumed not, since the issue is with clipboard) Unfortunately it's quite hard to find materials on controlling ppt's through excel vba

When pasting, it's likely that the process of copying the object to the clipboard hasn't finished. And so it's not available for pasting.

One way to deal with this might be to set up two simple separate routines. One routine to do the copying, and the other one to do the pasting.

In doing it this way, it may force VBA to wait until one process is finished before moving onto the next one.

Here's the routine to copy the range...

Sub CopyPivotTableRangeToClipboard(ByVal target As Range)
    target.Copy
End Sub

Here's the routine to paste the range into the slide...

Sub PasteRangeIntoSlide(ByVal theSlide As Object)
    theSlide.Shapes.PasteSpecial DataType:=2
End Sub

Then you would call these procedures as follows...

CopyPivotTableRangeToClipboard ws.PivotTables("pivot").TableRange2

PasteRangeIntoSlide MySlide

EDIT

If separating the copy and paste processing into two separate procedures doesn't help, you can additionally try pausing the macro for 1, 2, or 3 seconds, along with calling DoEvents, after each process. For this we can use the following procedure...

Sub PauseMacro(ByVal seconds_to_wait As Integer)
    Dim start_time As Single
    start_time = Timer
    Do
        DoEvents
    Loop Until Timer - start_time > seconds_to_wait
End Sub

Now your code would be as follows...

CopyPivotTableRangeToClipboard ws.PivotTables("pivot").TableRange2

PauseMacro 3 'seconds to wait (change as desired)

PasteRangeIntoSlide MySlide

PauseMacro 3 'seconds to wait (change as desired)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM