Exporting PowerPoint image shapes from VBA

M

Mike Clayton

Hi,

I've got two separate problems trying to export images from a PowerPoint
presentation. I can't see anything that might help with either on MSDN (or
anywhere else) so I'm guessing the answer is no to both, but here's hoping...

1. If I "Right-Click -> Save as Picture..." a picture shape I can extract
the original image that was inserted into the presentation back to disk. Is
there any way to do this from VBA? If I record a macro to do this the
resulting code exports all of the slides in the presentation instead of just
the selected shape.

2. Is there any way to export the background image ("Format -> Background ->
Fill Effects -> Picture") of a slide, or discover the filename of the image
that was originally inserted as the background?

Any ideas would be appreciated.

Regards,

Mike
 
S

Steve Rindsberg

I've got two separate problems trying to export images from a PowerPoint
presentation. I can't see anything that might help with either on MSDN (or
anywhere else) so I'm guessing the answer is no to both, but here's hoping...

1. If I "Right-Click -> Save as Picture..." a picture shape I can extract
the original image that was inserted into the presentation back to disk. Is
there any way to do this from VBA? If I record a macro to do this the
resulting code exports all of the slides in the presentation instead of just
the selected shape.

Use the shape's .Export method
In order to do this, you'll probably need to go into the Object Browser first,
rightclick and choose Show Hidden Members.
2. Is there any way to export the background image ("Format -> Background ->
Fill Effects -> Picture") of a slide, or discover the filename of the image
that was originally inserted as the background?

Not that I know of, at least.

Another approach to all of this is publishing the presentation as a web page
and digging the image files out of the resulting rubble.... er... collection of
files.
 
M

Mike Clayton

Hi Steve,

Thanks for that. Had a play with the Export method (after making hidden
methods visible) and it looks like what I'm after. It still doesn't allow a
byte-for-byte export of the original image but it's near enough as makes no
odds for my purposes.

I found an answer to part 2 as well - the background image is tucked away as
a ShapeRange under the Background property of Slide and Master objects so you
can just Export the first item from that in that the same way.

Sample code attached below.

Thanks again,

Mike




' exports the currently selected picture shape's image.
' (removes any resizing and cropping before exporting).
Public Sub ExportOriginalImage()
Dim objShape As PowerPoint.Shape
Dim objDuplicate As PowerPoint.Shape
' get a reference to the shape we want to export
Set objShape = PowerPoint.ActiveWindow.Selection.ShapeRange(1)
If Not (objShape.Type = msoPicture) Then Err.Raise 1000, , "Shape must
be a msoPicture"
' create a duplicate so we can reset some properties without upsetting
the selected shape
Set objDuplicate = objShape.Duplicate.Item(1)
' remove any cropping
objDuplicate.PictureFormat.CropLeft = 0
objDuplicate.PictureFormat.CropRight = 0
objDuplicate.PictureFormat.CropTop = 0
objDuplicate.PictureFormat.CropBottom = 0
' reset the image size
objDuplicate.ScaleHeight 1, msoTrue
objDuplicate.ScaleWidth 1, msoTrue
' export the duplicate shape
objDuplicate.Export "c:\temp\export original.png", ppShapeFormatPNG,
objDuplicate.Width, objDuplicate.Height, ppScaleXY
' we've finished with the duplicate shape so delete it
objDuplicate.Delete
End Sub

' exports the background from the first slidemaster in the current
presentation.
Public Sub ExportBackgroundImage()
Dim objPresentation As PowerPoint.Presentation
Dim objMaster As PowerPoint.Master
Dim objShape As PowerPoint.Shape
' get a reference to the shape we want to export
Set objPresentation = PowerPoint.ActivePresentation
Set objMaster = objPresentation.SlideMaster
Set objShape = objMaster.Background.Item(1)
' export the background
objShape.Export "c:\temp\export background.png", ppShapeFormatPNG,
objPresentation.PageSetup.SlideWidth, objPresentation.PageSetup.SlideHeight,
ppScaleXY
End Sub
 
S

Steve Rindsberg

Hi Steve,

Thanks for that. Had a play with the Export method (after making hidden
methods visible) and it looks like what I'm after. It still doesn't allow a
byte-for-byte export of the original image but it's near enough as makes no
odds for my purposes.

It won't be the original for a couple of reasons; one is that you have to specify
the export resolution, but you don't know the size of the original image. And
really, PowerPoint doesn't necessarily store the original image; images are stored
internally as PNG, GIF or JPG, depending on the situation.

But if you want the closest thing to the original, do the HTML save as trick;
that'll at least get you the image at original resolution, if not necessarily in its
original format.

Thanks for posting the code ... good stuff.
 
P

Peter Huang [MSFT]

Hi Mike,

Thanks for your code sharing and Steve's suggestion in the community.
If you still have any other concern on this issue, please feel free to post
here and I am glad to be of assistance.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top