PowerPoint VBA - 粘贴特殊内容(增强型图元文件)错误

6

我正在使用PowerPoint 2003 SP3中的宏来查找Excel工作簿中指定的图表,将其复制,然后将其作为增强型图元文件粘贴到当前幻灯片中,最终用以下代码行实现:

Application.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile

As 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.


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.

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.


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.

Application.ActiveWindow.Panes(2).Activate


问题很可能是因为失去了对PowerPoint的焦点,因此没有ActiveWindow。如果您发布有关从Excel抓取的代码,那可能会有所帮助。 - Todd Main
@Otaku - 感谢你的建议。我正在尝试找出再次激活窗口的正确方法。假设这是问题所在,但我仍然没有成功。再次感谢。非常感激。 - variant
我自己尝试了一下,但是“Activate”方法仍然会随机出现错误。我使用了Kuba的建议,现在它完美地运行了。 - lovechillcool
3个回答

3

这很可能是因为在Excel和PowerPoint之间切换导致PowerPoint失去了焦点,因此没有ActiveWindow可以粘贴,或者ActiveWindow变成了PowerPoint中的不同Pane,比如幻灯片浏览模式或备注模式。


@varient:明白了。在提供任何建议之前,我得先尝试复制这个问题。让我花点时间 - 我现在在夏威夷 :) - Todd Main
谢谢。请将您的“失去焦点”消息发布为答案,以便我可以在此问题上给您加分。你说得对。解决方案是添加一行代码,告诉它重新选择主窗口。看起来它要么进入幻灯片浏览器窗格,要么进入备注窗格。Application.ActiveWindow.Panes(2).Activate - variant
@variant:明白了,很高兴听到问题被发现并解决了。我已经修改了上面的答案。 - Todd Main

2
我遇到了同样的问题。我有一个宏,可以将许多图形导出到PowerPoint中。但是一些复制粘贴操作以与上述相同的错误结束(运行时错误“-2147188160(80048240)”:)。
我意识到PasteSpecial错误不是由于失去焦点,而是由于失去剪贴板。
因此,解决方案是重新复制区域到剪贴板中,例如:
On Error GoTo paste_error
ppSlide.Shapes.PasteSpecial(2, link:=RangeLink).Select
...
paste_error:
    Worksheets(SheetName).Range(RangeName).copy
    Resume

也许这可以帮助某些人……

0

我曾经遇到同样的错误,并尝试了许多这里的解决方案。最后对我有效的是一个非常简单的方法。我认为它之所以有效,是因为第一行保存了幻灯片的引用。粘贴选项可以是增强型图元文件而不是位图。

    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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接