如何将两张 PowerPoint 幻灯片导出为 PDF 文件?

How to export two PowerPoint slides to PDF file?

在此站点中,我找到了将最后一张幻灯片打印为 PDF 的代码。

Sub PDFtesti()
    
timestamp = Now()
Dim PR As PrintRange
Dim lngLast As Long
Dim lngFirst As Long
Dim savePath As String
Dim PrintPDF As Integer
Dim name As String

name = ActivePresentation.Slides(2).Shapes("TextBox1").OLEFormat.object.Text

savePath = "C:\Powerpoint\" & Format(timestamp, "yyyymmdd-hhnn") & " - " & name & ".pdf"

lngLast = ActivePresentation.Slides.Count

With ActivePresentation.PrintOptions
    .Ranges.ClearAll
Set PR = .Ranges.Add(lngLong, lngLong)
End With

ActivePresentation.ExportAsFixedFormat _
Path:=savePath, _
FixedFormatType:=ppFixedFormatTypePDF, _
PrintRange:=PR, _
Intent:=ppFixedFormatIntentScreen, _
FrameSlides:=msoTrue, _
RangeType:=ppPrintSlideRange 

End Sub

我想打印两张幻灯片:幻灯片编号 2 和最后一页。

我试过了

Set PR = .Ranges.Add(lngLong, lngLong)
Set PR = .Ranges.Add(2, 2)

Set PR = .Ranges.Add(Array("lngLong, lngLong" & "2,2")

更新了解决方案。为了使打印特定幻灯片更加舒适,我决定将它们放入一个变量中 (slidesToPrint)。所有未放入此变量的幻灯片都在打印前隐藏(因此不打印)。打印后隐藏顺序恢复为原始顺序。因此,必须打印的幻灯片列在这一行中:
slidesToPrint = Array(2, lngLast)

完整代码:

Sub PDFtesti()

Dim timestamp As Date
Dim PR As PrintRanges
Dim lngLast As Long
Dim lngFirst As Long
Dim savePath As String
Dim PrintPDF As Integer
Dim name As String
Dim originalHides() As Long
Dim slidesToPrint() As Variant
Dim i As Variant

timestamp = Now()
With ActivePresentation
    name = .Slides(2).Shapes("TextBox1").OLEFormat.Object.Text
    savePath = "C:\Powerpoint\" & Format(timestamp, "yyyymmdd-hhnn") & " - " & name & ".pdf"
    lngLast = .Slides.Count
    .PrintOptions.Ranges.ClearAll
    
    ' Slides to print are put here (inside parentheses)
    slidesToPrint = Array(2, lngLast)
    
    ReDim originalHides(1 To lngLast)
    For i = 1 To lngLast
      originalHides(i) = .Slides(i).SlideShowTransition.Hidden
      .Slides(i).SlideShowTransition.Hidden = -1
    Next
    For Each i In slidesToPrint()
      .Slides(i).SlideShowTransition.Hidden = 0
    Next
    .ExportAsFixedFormat _
        Path:=savePath, _
        FixedFormatType:=ppFixedFormatTypePDF, _
        Intent:=ppFixedFormatIntentScreen, _
        FrameSlides:=msoTrue
    For i = 1 To lngLast
      .Slides(i).SlideShowTransition.Hidden = originalHides(i)
    Next
End With

End Sub

在您的代码中使用 PR As PrintRange。但是因为你想要两个页面,第二个和最后一个,你将需要两个范围,它不是 PrintRange 类型,而是 PrintRanges 类型。在这种情况下你会这样做:

.Ranges.Add(2, 2)
.Ranges.Add(lngLast, lngLast)

但这行不通,因为函数ExportAsFixedFormat只接受PrintRange,不接受PrintRanges。一种选择是先使用 PrintRanges(1)(2, 2) 将两张幻灯片打印到单独的文件,然后使用 PrintRanges(2)(lngLast, lngLast)。但这并不是你真正想要的。

解决方案。它使用PR(1)PRPrintRanges类型,而PR(1)PrintRange)。 PR(1) 是最后 两张 幻灯片对应的范围。诀窍是在打印之前将第二张幻灯片移动到 lngLast - 1 位置(最后一张幻灯片),然后在打印之后 return 将其移动到正确的位置。

这会将第二张幻灯片移动到最后一张幻灯片的前一张:
.Slides(2).MoveTo lngLast - 1
这个return把它放到原来的位置:
.Slides(lngLast - 1).MoveTo 2

完整代码:

Sub PDFtesti()

Dim timestamp As Date
Dim PR As PrintRanges
Dim lngLast As Long
Dim lngFirst As Long
Dim savePath As String
Dim PrintPDF As Integer
Dim name As String

timestamp = Now()
With ActivePresentation
    name = .Slides(2).Shapes("TextBox1").OLEFormat.Object.Text
    savePath = "C:\Powerpoint\" & Format(timestamp, "yyyymmdd-hhnn") & " - " & name & ".pdf"
    lngLast = .Slides.Count
    Set PR = .PrintOptions.Ranges
    PR.ClearAll
    PR.Add lngLast - 1, lngLast
    
    .Slides(2).MoveTo lngLast - 1
    .ExportAsFixedFormat _
        Path:=savePath, _
        FixedFormatType:=ppFixedFormatTypePDF, _
        PrintRange:=PR(1), _
        Intent:=ppFixedFormatIntentScreen, _
        FrameSlides:=msoTrue, _
        RangeType:=ppPrintSlideRange
    .Slides(lngLast - 1).MoveTo 2
End With

End Sub