PowerPoint VBA - Paste Special (Enhanced Metafile) bug - vba

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

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.

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

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

Related

Word 2016 Error copying shape to header. Method 'Paste' of object 'Range' failed (80010108)

I have Word 2016 MSO (16.0.4266.1001), 64-bit
I have a document that has two sections with different headers. In the first section, in the header, there is a shape with text that needs to be copied into the header of the second section.
In Word 2010 I had some code that worked.
Sub TEST()
Set hHeaderRange = ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary).Range
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.Item(1).Select
Selection.Copy
hHeaderRange.Paste
End Sub
But in Word 2016, this code gives an error (see below) on the line hHeaderRange.Paste
When you click the End button, Word crashes and restarts in an attempt to restore the last saved version of the document.
Moreover, if you stop the code before the line hHeaderRange.Paste, then you can go to the header of the second section and click Paste, and the figure will be inserted.
I also tried PasteSpecial, same thing.
If you specify in the code to insert a shape on the page, there are no problems.
Can anyone please tell me how to solve this problem?
The task is simple, you need to copy the shape from the header of one section to the header of another section.
The strangest thing is that Word is crashing!
If anyone has other versions of Word 2016 (not 16.0.4266.1001) or Word 2019, please check the code.
I can confirm that the code you are using causes Word to crash (Version 2201 Build 16.0.14827.20158)
As there is no other content in the header you are copying from there is a workaround that is actually better than your original method. A Range object has a FormattedText property (read/write) that can be used instead of copying and pasting. So your code should be:
Sub TEST()
Dim hdr1 As Range, hdr2 As Range
Set hdr2 = ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary).Range
Set hdr1 = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range
hdr2.FormattedText = hdr1.FormattedText
End Sub

Exporting chart from Excel to Powerpoint using VBA

So after a bit of digging I got some help to find a code to export a chart from Excel to Powerpoint, problem is it is only selecting an object.
Sub ertert()
With New PowerPoint.Application
With .Presentations.Add
With .Slides.Add(1, 12)
ActiveSheet.ChartObjects(1).CopyPicture xlPrinter, xlPicture
.Shapes.Paste
.Shapes(1).Select
.Application.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
.Application.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
End With
End With
End With
End Sub
the object it's selecting is a chart/vlookup image result inside of a larger chart (the object itself is named) so basically it's only getting a small part of the item itself.
Question is where I went wrong in having it only select an item? Also I would like to know how to modify this code to add to an existing Powerpoint (assuming the existing one is already open).
What exactly are you trying to do?
If you are just looking to display a chart that updates based on the content of your excel workbook you can simply copy the chart across and it creates a link between the two. When both your presentation and workbook are open it will update the chart automatically with no need for VBA.

Error in script which copies charts from Excel to PowerPoint

I am attempting to call the below Sub in order to copy given chart to a specified PowerPoint presentation. However, when I run the macro which calls this Sub, the line indicated below returns the following error: "Object doesn't support this property or method." What's odd is that both Shapes and Slide do contain the methods which are called. As well, the bitmap is correctly copied to my clipboard and pastes into the slide before the error is called. You will find the Sub() below.
Sub copyChart(chrt As Chart, pres As PowerPoint.Presentation)
Dim curSlide As Slide, dummySlide As Slide
Set dummySlide= pres.Slides(2) 'Second slide is dummy slide.
Set curSlide = dummySlide.Duplicate(1) 'Duplicate dummy, set as current slide.
chrt.CopyPicture Appearance:=xlScreen, Format:=xlBitmap 'Copy the chart as a picture.
curSlide.Shapes.Paste '<-----------Error here.
End Sub
As well, I was hoping to provide a .txt file of my entire script, but was unsure how (it is a little lengthy to paste here). Thanks for your help.
(Note that this implementation is very similar to that at Paste Excel Chart into Powerpoint using VBA, further confusing me.)
I have had a lot of trouble in recent versions of Office. 2003 and earlier didn't have this problem, 2007 and 2010 had it a bit, and 2013 and 2016 have it in spades.
When you step through the code it works fine, but when you run it at full speed, it errors on the paste.
It's as if the copy doesn't have time to finish finish, so when you paste the clipboard doesn't have anything in it yet to paste.
Sometimes this helps:
chrt.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
DoEvents
curSlide.Shapes.Paste
DoEvents tells VBA to wait while background operations have a chance to finish up.
It looks like the error can be attributed to how VBA handles variables across different references. (In particular, how PPT VBA handles them.) I was able to get the macro to work by actively selecting/copying the charts. I will need to do a little more research to get why variables cause problems, but at least I know how tackle the problem.
Sub copyChart(curSlide As Slide)
Dim chr as ChartObject
Set chr = Sheets("CHARTSHEET").ChartObjects(1)
Sheets("CHARTSHEET").Select
ActiveChart.CopyPicture
curSlide.Shapes.PasteSpecial
End Sub
I like to use another method, I like to define an Object, then set it to the pasted Chart. Afterwards, it's much easier modifying the pasted Chart object's parameters inside PowerPoint (from Excel).
See code below:
Sub copyChart(curSlide As Slide)
Dim chr As ChartObject
Dim myChart As Object
Set chr = Sheets("CHARTSHEET").ChartObjects(1)
chr.Copy
' setting myChart object to the pasted chart (let's me later an easy way to modify it's parameters)
Set myChart = curSlide.Shapes.PasteSpecial(ppPasteBitmap, msoFalse) ' can change first parameter to other avaialabe formats : ppPasteGIF, ppPasteEnhancedMetafile, ppPasteOLEObject, etc.
' set different parameters for the pasted chart in PowerPoint slide
With myChart
.Left = 200
.Top = 200
End With
End Sub
In the code line:
Set myChart = curSlide.Shapes.PasteSpecial(ppPasteBitmap, msoFalse)
You can change the first parameter in brackets: ppPasteBitmap to many other avaialble formats (test them and see which one gives you the best result), such as: ppPasteGIF, ppPasteEnhancedMetafile, ppPasteOLEObject, etc.

vba code works well in 'Normal' view but dosen't work in 'slideshow' in powerpoint 2003

I have a presentation with 6 slides. Slide 5 contains vba / macro codes for rotatechart, change angle etc., The codes work well thru' vba editor 'RUN'in active document.(.ppt) but when put in 'Slideshow" mode codes don't work. Dbug msg: "Invalid request. no active document present". Pl help with code.
Sub MoveItInSlideShow()
With SlideShowWindows(1).Presentation.Slides(5).Shapes("Picture 2")
ActiveWindow.Selection.SlideRange.Shapes("Picture 2").Select
Application.Run "'Hora.ppt'!Slide7.CreateSpirograph"
end with
end sub
Thanks
This line is unnecessary, since you've already set a reference to the shape in the With statement previous:
ActiveWindow.Selection.SlideRange.Shapes("Picture 2").Select
But since you can't select anything in slideshow view, any attempt to .Select anything will cause an error, which is probably what you're seeing.
If the code here requires a selected shape, you'll need to rewrite it:
Hora.ppt'!Slide7.CreateSpirograph

Run-time error '-2147188160(80048240) DataType:=ppPasteEnhancedMetafile Error

Seems there is some bug. Can't resolve this problem, all code is running fine and I am able to see the AutoShape is getting copied from Excel file but it is not adding it to PowerPoint. Popping up an error Run-time error '-2147188160(80048240) View.Pastespecial : Invalid Request. The specified data type is unavailable
If Range("H" & i).Value = 1 And Range("B" & i).Value = "FRONT" Then
objPPT.Presentations(1).Slides(9).Select
objPPT.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile
Your code will be faster and possibly more reliable if you don't rely on selecting anything:
With objPPT.Slides(9).Shapes
Set objShape = .PasteSpecial(ppPasteEnhancedMetafile)(1)
With objShape
' set coordinates and such here
End With
End With
As to why you're getting the error message, try stopping the code after you've put something on the clipbard. Then switch to PowerPoint, use Paste Special to see what paste options are available. If EMF isn't one of them, that's your problem ... you're not putting anything in EMF format on the clipboard.
I had a similar issue, but I found a different solution; it may be specific to what I was doing though.
I setup a program where I would:
(manual) Copy an entire webpage that was a report on several performance metrics
(manual) Pasted it in to excel
Run the program to extract the values I want and then clear contents of the sheet I pasted them on.
Eventually after many tests, it would fail with this same automation error when I tried to access the sheet:
Sheets("PDX Paste").Activate
I was able to activate every other sheet except that particular one, even using the index value instead of the direct name reference. After googling to no success I found out that the copy and paste from the website was also pasting invisible controls. When I found this out I had 1,300+ shapes when I only expected 1 (the button I use to trigger the program). It was actually only apparent when a glitch - presumably due to so much memory being used to store these controls - displayed for a few seconds.
I ran the following code independently and then appended it to the end of my program when I do the cleanup of the data. The code goes through the sheet and deletes any shape that isn't the same type as my button. It would have to be adapted if the shapes you want to delete are the same type as the shapes you want to keep. It also becomes simpler if you don't have any shapes to keep.
Dim wsh As Worksheet
Set wsh = ActiveSheet
Dim i As Integer
For i = wsh.Shapes.Count To 1 Step -1
If wsh.Shapes(i).Type <> wsh.Shapes("UpdateDataButton").Type Then
wsh.Shapes(i).Delete
End If
Next i
I'm not sure this would solve this problem, but hopefully this can help others and prevent loss of time figuring out what may be causing this relatively vague error message.