在 Excel VBA 中复制图形时清除剪贴板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7793593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:17:37  来源:igfitidea点击:

Clear clipboard when copying graphs in Excel VBA

excelvbapowerpointcopy-paste

提问by reggie

I copy and paste several Excel graphs from Excel to Powerpoint using 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
' ...

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.

对象“Chartobject”的方法“Copy”失败。

Then Excel gets unresponsive and I have to restart it.

然后 Excel 没有响应,我必须重新启动它。

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.

更新:我已经尝试了上面提到的两种方法来清除剪贴板。复制和粘贴操作仍然不时失败。运行时错误是“-2147417848 (80010108)”。网上有一些关于这个错误的信息,所以我将从那里重新开始。

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

回答by Doc Brown

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

下面是一个通过 VBA 访问剪贴板的小例子:

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

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.

通过将一些空文本放入剪贴板来清除剪贴板。老实说,我不知道这是否会解决您原来的问题。

回答by DontFretBrett

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

尝试Application.CutCopyMode = 0在复制和粘贴操作之间进行放置。

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

0False表示“取消剪切或复制模式并删除移动边框”:http: //msdn.microsoft.com/en-us/library/office/ff839532.aspx

回答by Dan

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

我在复制和粘贴语句之间插入 application.cutcopymode = 0 没有成功。

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

当我
在 paste 和 close 语句之间插入 application.cutcopymode = 0 时,我成功了。

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

回答by Bhanu Sinha

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

在粘贴之前激活对象。我使用以下方法解决了

.Slides(3).shapes.activate

.Slides(3).shapes.activate

回答by Steve Rindsberg

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?

您没有提及您使用的是哪个版本的 Office。如果是 2007,您是否应用了 Service Pack 2 并检查了可能解决此问题的修补程序?

The original release of 2007 was awful.

2007 年的原始版本很糟糕。