5
votes

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:

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
3
The issue is very likely the loss of focus on PowerPoint, and therefore there is no ActiveWindow. If you post your code about grabbing from Excel, that may be helpful.Todd Main
@Otaku - thanks for your input. I'm trying to figure out the proper way to activate the window again. Assuming this is the problem, I'm still not having success. Thanks again. Very much appreciated.variant
I tried myself and the Activate method still got errors randomly. I used suggestions by Kuba and it works like charm.lovechillcool

3 Answers

3
votes

This is likely a loss of focus where switching between Excel and PowerPoint is causing PowerPoint to lose focus and therefore there is no ActiveWindow for PowerPoint to paste in to or the ActiveWindow becomes a different Pane in PowerPoint, such as the Slide Sorter or the Notes pane.

1
votes

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)':)

I realized that the PasteSpecial error was not due to the loss of focus, but due to the loss of clipboard.

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

0
votes

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.

    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