Reputation: 33
I need to write a macro to convert all grouped diagrams (shapes, arrows and text) in a PPT presentation to PNGs. (I am converting the PPTs using some eLearning software and the diagrams end up corrupt; and I need them to be PNGs because enhanced metafiles also present issues).
I've been using some slightly modified code from a macro that converts Pictures (enhanced meta files) to PNGs. All I did was change msoPicture to msoGroup:
Sub ConvertAllPicsToPNG()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
' modify the following depending on what you want to
' convert
Select Case oSh.Type
Case msoGroup
ConvertPicToPNG oSh
Case Else
End Select
Next
Next
End Sub
Sub ConvertPicToPNG(ByRef oSh As Shape)
Dim oNewSh As Shape
Dim oSl As Slide
Set oSl = oSh.Parent
oSh.Copy
Set oNewSh = oSl.Shapes.PasteSpecial(ppPastePNG)(1)
With oNewSh
.Left = oSh.Left
.Top = oSh.Top
Do
.ZOrder (msoSendBackward)
Loop Until .ZOrderPosition = .ZOrderPosition
End With
oSh.Delete
End Sub
I get the error "Shapes (unknown member)" on the line
Set oNewSh = oSl.Shapes.PasteSpecial(ppPastePNG)(1)
I suspect I'm having problems with VBA's Object Reference Model, as research has told me of GroupItems and GroupShapes, but I can't fathom it.
Upvotes: 2
Views: 3900
Reputation: 53663
I get this error in PPT 2010: "Shapes (unknown member) : Invalid request. Clipboard is empty or contains data which may not be pasted here."
We both notice there is "Shape 125" when you zoom out or use the Selection Pane:
After a lot of trial and error (I thought the nesting might be a problem, and tried to un-nest them -- successfully, but the error still happened) I noticed that each of them had a height of 0
. If I changed that to any positive value, success!
So here is the fix -- call a new function to make sure shapes have height > 0:
For Each oSh In oSl.Shapes
' modify the following depending on what you want to
' convert
Select Case oSh.Type
Case msoGroup
'Ensure each grouped shape has h/w of at least "1"
FixShape oSh
ConvertPicToPNG oSh
Case Else
Here is the function:
Function FixShape(ByRef oSh As Shape)
Dim s As Shape
'## Iterate the GroupItems collection and ensure minimum height/width
' for converion to png/jpg/etc.
For Each s In oSh.GroupItems
If s.Height = 0 Then s.Height = 1
If s.Width = 0 Then s.Width = 1
'Recursive
If s.Type = msoGroup Then
Set s = FixShape(s)
End If
Next
Set FixShape = oSh
End Function
Here is the final output which converts the shapes to PNG:
Root Cause of this Error
It seems you are not able to paste shapes with height/width of 0, as PNG format (although you can paste them as Shapes). This seems to be an intentional limitation, but unfortunately the error message is ambiguous.
Solution to this Error
Ensure that shapes have minimum dimensions of 1x1 before trying to paste as an image format (PNG, JPG, etc.)
While you were able to resolve the problem by deleting the offending shape, this should help you so that you don't have to search for those off-pane shapes or try to troubleshoot this again in the future.
Upvotes: 3