Clear clipboard when copying graphs in Excel VBA

2019-08-07 10:09发布

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

5条回答
戒情不戒烟
2楼-- · 2019-08-07 10:54

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-07 10:54

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.

查看更多
干净又极端
4楼-- · 2019-08-07 10:57

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

.Slides(3).shapes.activate

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-08-07 11:00

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

查看更多
甜甜的少女心
6楼-- · 2019-08-07 11:06

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
查看更多
登录 后发表回答