PowerPoint VBA - 特殊粘贴(增强型图元文件)错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3260717/
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
PowerPoint VBA - Paste Special (Enhanced Metafile) bug
提问by variant
I am using a macro in PowerPoint 2003 SP3 to find a specified chart in an Excel workbook, copy it, and then paste it into the current slide as an Enhanced Metafile with, ultimately, the following line of code:
我在 PowerPoint 2003 SP3 中使用宏在 Excel 工作簿中查找指定图表,将其复制,然后将其作为增强型元文件粘贴到当前幻灯片中,最终使用以下代码行:
Application.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafileAs often as it works, I also receive the following error:
只要它有效,我也会收到以下错误:
Run-time error '-2147188160 (80048240)': View (unknown member) : Invalid request. The specified data type is unavailable.
If I end the macro and attempt to manually Paste Special as Enhanced Metafile, I have no problem, so it's not as though the clipboard object or the pastespecialtype is invalid.
如果我结束宏并尝试手动将特殊粘贴为增强型元文件,则没有问题,因此剪贴板对象或粘贴特殊类型并非无效。
Has anyone else experienced this? Do you have a solution or a workaround? There are few results and no solutions in a Google search on this error.
有没有其他人经历过这种情况?您有解决方案或解决方法吗?Google 搜索中关于此错误的结果很少,也没有解决方案。
Update
更新
The general code is as follows:
一般代码如下:
Set presPPTCurrent = ActivePresentation
Set objXLApp = GetObject(, "Excel.Application")
''#Find the target chart and copy it to the clipboard
With objXLApp
''#This part works - if I go to Excel, I can see that the chart is copied
End With
''#Now paste in the chart as an Enhanced Metafile
presPPTCurrent.Application.Activate
Application.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile
Note that this is in a Sub to which a Shape is passed (the Shape being passed is used as a reference to find the chart in Excel). I've realized that it only bugs when I attempt to reuse this sub on multiple shapes passed from a For Next loop in another Sub.
请注意,这是在传递 Shape 的 Sub 中(传递的 Shape 用作在 Excel 中查找图表的参考)。我已经意识到,当我尝试在从另一个 Sub 中的 For Next 循环传递的多个形状上重用这个 sub 时,它只会出现错误。
However, if I pass a single Shape to this Sub with via another Sub and then re-run the Sub that passes multiple Shapes, it runs fine.
但是,如果我通过另一个 Sub 将单个 Shape 传递给这个 Sub,然后重新运行传递多个 Shape 的 Sub,它运行良好。
Solution
解决方案
Per Otaku's mention, the macro was losing its focus on the Slide Pane. Telling it to re-select the Slide Pane solved the issue.
根据 Otaku 的说法,宏正在失去对幻灯片窗格的关注。告诉它重新选择滑动窗格解决了这个问题。
Application.ActiveWindow.Panes(2).Activate采纳答案by Todd Main
This is likely a loss of focus where switching between Excel and PowerPoint is causing PowerPoint to lose focus and therefore there is no ActiveWindowfor PowerPoint to paste in to or the ActiveWindowbecomes a different Panein PowerPoint, such as the Slide Sorter or the Notes pane.
这可能是失去焦点,在 Excel 和 PowerPoint 之间切换导致 PowerPoint 失去焦点,因此ActiveWindowPowerPoint 无法粘贴到 PowerPoint 中或在 PowerPoint 中ActiveWindow变得不同Pane,例如幻灯片浏览或备注窗格。
回答by kuba
I was experiencing the same issue. I have macro which exports a lot of graphs to the PowerPoint. But some copy-paste actions end with the same error as above (Run-time error '-2147188160 (80048240)':)
我遇到了同样的问题。我有将大量图形导出到 PowerPoint 的宏。但是一些复制粘贴操作以与上述相同的错误结束(运行时错误 '-2147188160 (80048240)':)
I realized that the PasteSpecial error was not due to the loss of focus, but due to the loss of clipboard.
我意识到 PasteSpecial 的错误不是因为失去焦点,而是因为失去了剪贴板。
The solution is therefore recopying the area to the clipboard again like:
因此,解决方案是再次将该区域重新复制到剪贴板,例如:
On Error GoTo paste_error
ppSlide.Shapes.PasteSpecial(2, link:=RangeLink).Select
...
paste_error:
Worksheets(SheetName).Range(RangeName).copy
Resume
Maybe this help somebody...
也许这有助于某人...
回答by swmcdonnell
I was getting the same errors and experimented with many of the solutions on here. What ended up working for me was something very simple. I think it works because of the first line that saves a reference to the slide. The paste option could be EnhancedMetaFile instead of Bitmap.
我遇到了同样的错误,并在这里尝试了许多解决方案。最终对我来说有效的是非常简单的事情。我认为它之所以有效是因为第一行保存了对幻灯片的引用。粘贴选项可以是 EnhancedMetaFile 而不是 Bitmap。
Sub Macro()
Set sld = Application.ActiveWindow.View.Slide
With GetObject(, "Excel.Application")
.Workbooks(1).Sheets(1).Shapes(1).Copy
End With
sld.Shapes.PasteSpecial DataType:=ppPasteBitmap
End Sub

