好吧,经过几个月的搜索,我终于找到了答案,我想我会在这里分享它,并根据我想要实现的目标添加一些附加信息。大多数代码都是由在网上找到的随机部分和我自己的一些劣质代码提供的。
Sub Generate_PDF_Cert()
'This function saves the last slide as a PDF file with a time stamp and the users name who completed the induction.
timestamp = Now()
Dim PR As PrintRange
Dim lngLast As Long
Dim savePath As String
Dim PrintPDF As Integer
'Location of saved file
savePath = Environ("USERPROFILE") & "\Desktop\Induction\Certificates\" & Format(timestamp, "yyyymmdd-hhnn") & "_" & FirstNameX & "_" & LastNameX & ".pdf"
lngLast = ActivePresentation.Slides.Count
With ActivePresentation.PrintOptions
.Ranges.ClearAll ' always do this
Set PR = .Ranges.Add(Start:=lngLast, End:=lngLast)
End With
ActivePresentation.ExportAsFixedFormat _
Path:=savePath, _
FixedFormatType:=ppFixedFormatTypePDF, _
PrintRange:=PR, _
Intent:=ppFixedFormatIntentScreen, _
FrameSlides:=msoTrue, _
RangeType:=ppPrintSlideRange
'Prompt user of file location and option to print.
PrintPDF = MsgBox("A PDF file of this certificate has been saved to: " & vbCrLf & savePath & vbCrLf & vbCrLf & "Would you like to print a copy also?", vbYesNo, "PDF File Created")
If PrintPDF = 6 Then Call Print_Active_Slide
End Sub
这样 PDF 就创建得既漂亮又简单。它基本上会获取节目的最后一张幻灯片,然后仅将该幻灯片导出为 PDF。这可以更改为特定幻灯片或幻灯片范围。然后还有一个选项可以使用以下功能打印所选幻灯片:
Sub Print_Active_Slide()
' This code determines what slide is currently visible in the
' slide show and then it clears the print range and prints out the
' current slide.
' Declare lSldNum as a long integer.
Dim lSldNum As Long
' Assign lSldNum to the current slide number.
lSldNum = SlideShowWindows(1).View.Slide.SlideNumber
' Set the print options for the active presentation.
With ActivePresentation.PrintOptions
' Set RangeType to print a slide range.
.RangeType = ppPrintSlideRange
' Delete old print range settings.
.Ranges.ClearAll
' Set Ranges to the new range for the current slide.
.Ranges.Add lSldNum, lSldNum
End With
' Using the current print settings print the slide to the default
' printer.
ActivePresentation.PrintOut
MsgBox "The file has been sent to the default printer", vbOKOnly, "Print Job Sent"
End Sub