在 Excel VBA 中复制图形时清除剪贴板
我使用 VBA 将多个 Excel 图表从 Excel 复制并粘贴到 Powerpoint:
'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
' ...
有时,复制图表会失败。除了可能是内存问题之外,我对此没有任何解释。 我收到的错误消息:
对象“Chartobject”的“复制”方法失败。
然后 Excel 无响应,我必须重新启动它。
我可以做什么来防止这种情况发生? 也许我可以在复制和粘贴操作之间清除剪贴板?
更新: 我已经尝试过上述两种方法来清除剪贴板。副本 &粘贴操作仍然有时会失败。运行时错误为“-2147417848 (80010108)”。 网上有一些关于此错误的信息,所以我将从那里开始。
更新(最终): 我想我通过将此代码放在复制和粘贴图表的部分前面解决了问题。该错误没有再次出现。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面是一个通过 VBA 访问剪贴板的小示例:
http://word.mvps.org /faqs/macrosvba/ManipulateClipboard.htm
通过在其中放入一些空文本来清除剪贴板。老实说,我不知道这是否能解决你原来的问题。
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.
尝试在复制和粘贴操作之间放置
Application.CutCopyMode = 0
。0
或False
表示“取消剪切或复制模式并删除移动边框”:http://msdn.microsoft.com/en-us/library/office /ff839532.aspx
Trying putting
Application.CutCopyMode = 0
between the copy and paste operations.0
orFalse
means "Cancels Cut or Copy mode and removes the moving border":http://msdn.microsoft.com/en-us/library/office/ff839532.aspx
您没有提及您正在使用哪个版本的 Office。如果是 2007 年,您是否应用了 Service Pack 2 并检查了可能解决此问题的修补程序?
2007 年的最初版本很糟糕。
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.
我插入失败
应用程序.cutcopymode = 0
复制和粘贴语句之间。
我插入的时候成功了
application.cutcopymode = 0
在粘贴和关闭语句之间。
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.
粘贴前激活对象。我使用以下
.Slides(3).shapes.activate
解决了Activate the object before you paste. I solved using the following
.Slides(3).shapes.activate
我也有类似的问题;复制一个对象(一组形状)后,该对象保留在内存中。
Application.CutCopyMode = False
没有帮助。我添加了以下内容,并解决了问题:
希望它对某人有帮助。
I had a similar problem; after copying an object (group of shapes), the object remained in the memory.
Application.CutCopyMode = False
did not help.I added the following, and got rid of the problem:
Hope it helps someone.