Home » excel » Clear clipboard when copying graphs in Excel VBA

Clear clipboard when copying graphs in Excel VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I copy and paste several Excel graphs from Excel to Powerpoint using VBA:

'copy from excel worksheet
    Workbooks(Stats & ".xls").Worksheets(Stats).ChartObjects("graph4").Copy
'paste into powerpoint presentation
    .Slides(3).Shapes.Paste

' more code and more copy and pastes
' ...

Sometimes, copying the graphs fails. I have no explanation for this other than maybe memory problems.
The error message that I get:

Method ‘Copy’ of object ‘Chartobject’ failed.

Then Excel gets unresponsive and I have to restart it.

What can I do to prevent this?
Maybe I could clear the clipboard between the copy and paste operations?

Update:
I’ve tried the two mentioned ways to clear the clipboard. The copy & paste operation still fails from time to time. The run-time error is “-2147417848 (80010108)”.
There’s some info about this error on the net, so I’ll start over from there.

Update (Final):
I think I solved the problem by putting this code in front of the parts where the charts are copy and pasted. The error has not appeared again.

DoEvents 'lets the operating system clear / execute any backed up / queued events that it might have to execute.
'slow down the execution (to not get an error)
Workbooks(Stats & ".xls").Save
How to&Answers:

Trying putting Application.CutCopyMode = 0 between the copy and paste operations.

0 or False means “Cancels Cut or Copy mode and removes the moving border”:
http://msdn.microsoft.com/en-us/library/office/ff839532.aspx

Answer:

Here is a small example for accessing the clipboard by VBA:

http://word.mvps.org/faqs/macrosvba/ManipulateClipboard.htm

Clear the clipboard by putting some empty text into it. Honestly, I don’t know if this will solve your original problem.

Answer:

You don’t mention which version of Office you’re working in. If 2007, have you applied Service Pack 2 and checked for hotfixes that might address this issue?

The original release of 2007 was awful.

Answer:

I was unsuccessful with inserting
application.cutcopymode = 0
between copy and paste statements.

I was successful when I inserted
application.cutcopymode = 0
between the paste and close statements.

windows(ThisBook).activate: range(nextcell).select: activesheet.paste
application.cutcopymode = 0
windows(OtherBook).activate: activewindow.close

Answer:

Activate the object before you paste. I solved using the following

.Slides(3).shapes.activate