PowerPoint のすべてのスライドを画像化して新たなファイルにするVBAスクリプト

  • 私は macOS ユーザーである。
  • 時に、発表などでは据え置きに Windows PC を用いて発表しなければならないことがある。
  • macOS で作成した PowerPoint ファイルは Windows で表示が崩れることがある。
  • でも macOS で作成したい。
  • PowerPoint のすべてのスライドを画像化して新たなファイルにするVBAスクリプトを書けば解決かと思って ChatGPT を叩いたら以下のコードが出てきた。
  • 動いたので共有する。
Option Explicit

' Export all *visible* slides as high-resolution images, then create a new PPTX with image-only slides
Sub ExportSlidesToImagePptx()

    Dim srcPres As Presentation
    Dim newPres As Presentation
    Dim s As Slide
    Dim newSld As Slide
    Dim shp As Shape
    
    ' ==== SETTINGS ====
    Dim targetPPI As Long: targetPPI = 300   ' Recommended: 300–600 (higher values increase file size)
    Dim imgFormat As String: imgFormat = "PNG" ' Can also be "JPG"
    Dim keepTempFiles As Boolean: keepTempFiles = False ' Set True to keep exported image files
    
    Set srcPres = ActivePresentation
    
    ' Convert slide size (points) to pixels (PowerPoint uses 72 points per inch)
    Dim slideWpt As Single, slideHpt As Single
    Dim pxW As Long, pxH As Long
    slideWpt = srcPres.PageSetup.SlideWidth
    slideHpt = srcPres.PageSetup.SlideHeight
    pxW = CLng(slideWpt / 72# * targetPPI)
    pxH = CLng(slideHpt / 72# * targetPPI)
    
    ' Create temporary directory (macOS TMPDIR)
    Dim tmpBase As String, tmpDir As String
    tmpBase = Environ$("TMPDIR")
    If Right$(tmpBase, 1) <> "/" Then tmpBase = tmpBase & "/"
    tmpDir = tmpBase & "ppt_img_export/"
    
    On Error Resume Next
    Kill tmpDir & "*.*"
    RmDir tmpDir
    On Error GoTo 0
    
    MkDir tmpDir
    ' MsgBox "Temporary export folder: " & tmpDir, vbInformation, "Debug Info"
    
    ' Export only visible slides as images
    Dim i As Long, idxVisible As Long, f As String
    idxVisible = 0
    For Each s In srcPres.Slides
        If Not s.SlideShowTransition.Hidden Then
            idxVisible = idxVisible + 1
            f = tmpDir & "slide_" & Format$(idxVisible, "000") & IIf(UCase$(imgFormat) = "PNG", ".png", ".jpg")
            s.Export f, imgFormat, pxW, pxH
        End If
    Next s
    
    ' Create a new blank presentation
    Set newPres = Presentations.Add(msoTrue)
    With newPres.PageSetup
        .SlideWidth = slideWpt
        .SlideHeight = slideHpt
    End With
    
    ' Insert each exported image to a new blank slide
    Dim total As Long: total = idxVisible
    For i = 1 To total
        f = tmpDir & "slide_" & Format$(i, "000") & IIf(UCase$(imgFormat) = "PNG", ".png", ".jpg")
        Set newSld = newPres.Slides.Add(i, ppLayoutBlank)
        Set shp = newSld.Shapes.AddPicture( _
                FileName:=f, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
                Left:=0, Top:=0, Width:=newPres.PageSetup.SlideWidth, Height:=newPres.PageSetup.SlideHeight)
        shp.ZOrder msoBringToFront
        shp.Left = 0
        shp.Top = 0
        shp.Width = newPres.PageSetup.SlideWidth
        shp.Height = newPres.PageSetup.SlideHeight
    Next i
    
    ' Save the new presentation to Desktop
    Dim desktop As String
    desktop = Environ$("HOME") & "/Desktop/"
    On Error Resume Next
    newPres.SaveAs desktop & "Slides_as_Images_" & Format$(Now, "yyyymmdd_HHNNSS") & ".pptx"
    On Error GoTo 0
    
    ' Delete temporary files
    If Not keepTempFiles Then
        On Error Resume Next
        Kill tmpDir & "*.*"
        RmDir tmpDir
        On Error GoTo 0
    End If
    
    MsgBox "All *visible* slides have been exported as high-resolution images and reassembled into a new PPTX." & vbCrLf & _
           "PPI: " & targetPPI & " / Image size: " & pxW & " ? " & pxH & " px", vbInformation
End Sub