You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerPoint VBA技术问询:如何选择指定幻灯片或节并导出为MP4?

Hey Leo, let's tackle your two PowerPoint VBA questions about exporting MP4s—here's how to make this work, including fixing that section-export macro you tried modifying:


1. Export Slides 30–80 as MP4

PowerPoint VBA doesn't have a one-liner for exporting a subset of slides to MP4, but we can set a custom slide show range and then export that range as a video. Here's a complete macro:

Sub ExportSlides30To80AsMP4()
    Dim exportPath As String
    Dim targetPres As Presentation
    
    Set targetPres = ActivePresentation
    
    ' Set your output path and filename (adjust this as needed)
    exportPath = targetPres.Path & "\Slides30-80.mp4"
    
    ' Define the slide range we want to export
    With targetPres.SlideShowSettings
        .RangeType = ppShowSlideRange
        .StartingSlide = 30
        .EndingSlide = 80
    End With
    
    ' Export the defined range as MP4
    targetPres.SaveAs2 _
        FileName:=exportPath, _
        FileFormat:=ppSaveAsMP4, _
        EmbedTrueTypeFonts:=msoFalse ' Change to msoTrue if you need embedded fonts
End Sub

How this works:

  • We use SlideShowSettings to tell PowerPoint we only want to work with slides 30–80
  • The SaveAs2 method with ppSaveAsMP4 format exports that specific range directly as a video file

2. Export a Single Section as MP4

The macro you found (for PNG exports) uses the .Export method, which only works for image formats like PNG/JPG—this is why it broke when you tried switching to MP4. Instead, we need to:

  1. Find the start/end slide numbers of your target section
  2. Set a slide show range for those slides
  3. Export that range as MP4

First, keep your SectionIndexOf function (I added a small tweak to exit early when the section is found):

Function SectionIndexOf(sSectionName As String) As Long
    Dim x As Long
    With ActivePresentation.SectionProperties
        For x = 1 To .Count
            If .Name(x) = sSectionName Then
                SectionIndexOf = x
                Exit Function ' Exit loop early once we find the section
            End If
        Next
    End With
    ' Return 0 if the section isn't found
    SectionIndexOf = 0
End Function

Then use this macro to export your target section as MP4:

Sub ExportSectionAsMP4()
    Dim targetSectionName As String
    Dim targetSectionIndex As Long
    Dim startSlide As Long
    Dim endSlide As Long
    Dim exportPath As String
    Dim targetPres As Presentation
    
    Set targetPres = ActivePresentation
    targetSectionName = "Test" ' Replace this with your section name
    
    ' Get the index of our target section
    targetSectionIndex = SectionIndexOf(targetSectionName)
    If targetSectionIndex = 0 Then
        MsgBox "Couldn't find a section named '" & targetSectionName & "'!", vbExclamation
        Exit Sub
    End If
    
    ' Get the first and last slide numbers of the section
    With targetPres.SectionProperties
        startSlide = .FirstSlide(targetSectionIndex)
        endSlide = .LastSlide(targetSectionIndex)
    End With
    
    ' Set output path (customize this to your needs)
    exportPath = targetPres.Path & "\Section-" & targetSectionName & ".mp4"
    
    ' Define the slide range for the section
    With targetPres.SlideShowSettings
        .RangeType = ppShowSlideRange
        .StartingSlide = startSlide
        .EndingSlide = endSlide
    End With
    
    ' Export the section as MP4
    targetPres.SaveAs2 _
        FileName:=exportPath, _
        FileFormat:=ppSaveAsMP4, _
        EmbedTrueTypeFonts:=msoFalse
End Sub

Key changes from your original PNG macro:

  • We use SectionProperties.FirstSlide and LastSlide to get the full slide range of the section (no need to loop through every slide)
  • We set the slide show range to match the section's slides
  • We use SaveAs2 with ppSaveAsMP4 instead of .Export (since .Export only handles images)

Quick Notes:

  • Make sure your PowerPoint installation supports MP4 exports (most modern Office versions do, but you'll need the right video codecs installed)
  • Save your presentation first before running the macro—this avoids issues with the Path property
  • If you need to adjust video quality/resolution, you can use the CreateVideo method instead (it's more flexible but requires additional parameters for timing and settings)

内容的提问来源于stack exchange,提问作者Leo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:23:47