Search code examples
excelvbapowerpointcopy-paste

Clear clipboard when copying graphs in Excel VBA


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

Solution

  • 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