使用VBA更改Powerpoint演示文稿中的照片文件类型?

时间:2017-08-18 17:56:35

标签: vba powerpoint powerpoint-vba image-conversion

我有一个powerpoint演示文稿,每张幻灯片上的照片都是一个大文件大小(.EMF)。我想将它们全部更改为.PNG,以使最终文件大小更小。

到目前为止,我有这个:

Sub ConvertShapeToPNG()
    Dim osh As Shape
    Set osh = ActiveWindow.Selection.ShapeRange(1)
    osh.Copy
    ActiveWindow.Selection.SlideRange.Shapes.PasteSpecial ppPastePNG
    osh.Delete
End Sub

这将拍摄幻灯片上选择的图片并将其替换为PNG。 我在整个演示文稿中无法完成这项工作,因为它要我先选择图片。

这是完整演示文稿版本的代码:

Sub ConvertAllShapesToPNG()

Dim osld As Slide
Dim osh As Shape

For Each osld In ActivePresentation.Slides
    For Each osh In osld.Shapes
        Set osh = ActiveWindow.Selection.ShapeRange(1)
        osh.Copy
        ActiveWindow.Selection.SlideRange.Shapes.PasteSpecial ppPastePNG
        osh.Delete
    Next
Next osld

End Sub

任何人都可以帮我在整个演示文稿中正确运行此代码吗? 谢谢!

编辑:如果照片复制到与原始照片相同的位置而不是复制到幻灯片的中心,这也是理想的,但我还没有尝试过。

1 个答案:

答案 0 :(得分:1)

我自己想出来了!

如果有人好奇,这是代码:

Sub ConvertAllShapesToPNG()
'PURPOSE: Change Pictures into .PNG images

Dim shp As Shape
Dim sld As Slide
Dim pic As Shape
Dim shp_left As Double
Dim shp_top As Double

'Loop Through Each Slide in ActivePresentation
  For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes

      If shp.Type = msoPicture Then
        'Retrieve current positioning
          shp_left = shp.Left
          shp_top = shp.Top

        'Copy/Paste as .PNG Picture
          shp.Copy

          sld.Shapes.PasteSpecial DataType:=ppPastePNG

          Set pic = sld.Shapes(sld.Shapes.Count)

        'Delete Linked Shape
          shp.Delete

        'Reposition newly pasted picture
          pic.Left = shp_left
          pic.Top = shp_top

      End If

    Next shp
  Next sld

  MsgBox "All photos are now .PNGs"

End Sub