【问题标题】:powerpoint vba export certain slide to pdfpowerpoint vba 将某些幻灯片导出为 pdf
【发布时间】:2016-11-20 16:20:56
【问题描述】:

调用此函数时,我正在尝试将选定的幻灯片导出为 pdf。

此代码运行良好,但将整个幻灯片显示为 PDF。

Sub Export_to_PDF()
    ActivePresentation.ExportAsFixedFormat ActivePresentation.Path & "\" & "ExportedFile" & ".pdf", ppFixedFormatTypePDF, ppFixedFormatIntentPrint
End Sub

如何更改上述代码,以便指定要导出为 PDF 的幻灯片编号或幻灯片系列。我需要此代码才能从幻灯片视图运行。

非常感谢。

【问题讨论】:

    标签: vba pdf powerpoint export-to-pdf


    【解决方案1】:

    好的,经过几个月的搜索,我终于找到了答案,并认为我会在这里分享它,并根据我想要实现的目标提供一些额外的信息。大部分代码都是由网上随机找到的部分和我自己的一些拙劣的代码提供的。

    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
    

    【讨论】:

      【解决方案2】:

      谢谢,我遇到了同样的问题,这是我的解决方案(在 pdf 中保留超链接):

      Sub Main()
      
          'Hide all slides
          For i = 1 To ActivePresentation.Slides.Count
              ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoTrue
          Next i
      
          'Show your slides (for example: slide 2 + 4)
          ActivePresentation.Slides(2).SlideShowTransition.Hidden = msoFalse
          ActivePresentation.Slides(4).SlideShowTransition.Hidden = msoFalse
      
          'Save location
          Dim filePath As String
          filePath = "C:\Users\YOUR_NAME\Desktop\fileName.pdf"
          ActivePresentation.SaveAs filePath, ppSaveAsPDF
      
          'Show all slides again
          For i = 1 To ActivePresentation.Slides.Count
              ActivePresentation.Slides(i).SlideShowTransition.Hidden = msoFalse
          Next i
      
      End Sub
      

      【讨论】:

      • 很好的答案。比我拼凑的代码更简单。我想我会从现在开始使用它。感谢您提供新信息。
      • 如果有人想将每张幻灯片保存在单独的 pdf 中,我调整了此处提供的答案以适应略有不同的用例 (stackoverflow.com/questions/17929124/…)。希望它能拯救别人的一天。
      【解决方案3】:

      您还可以使用以下方法将选定的幻灯片保存为 PDF 文件:

      Sub Test()
      
          'select slides
          ActivePresentation.Slides.Range(Array(1, 2, 4, 6, 9, 11)).Select
          'save selected slides to a PDF file
          ActivePresentation.ExportAsFixedFormat Path:="C:\Path\xxx.pdf", _
              FixedFormatType:=ppFixedFormatTypePDF, _
              Intent:=ppFixedFormatIntentPrint, _
              OutputType:=ppPrintOutputSlides, _
              RangeType:=ppPrintSelection
      
      End Sub
      

      但是,如果您从 Excel VBA 调用此函数,则可能无法获取选定的幻灯片。因此,如果您从 Powerpoint 外部调用此函数,请务必先激活幻灯片预览窗格,如下所示:

          Set pptApp = ActivePresentation.Parent 'Powerpoint Application
          pptApp.ActiveWindow.Panes(1).Activate 'You can skip this if you're already in Powerpoint
      

      【讨论】:

      • 这是杀手锏,超级简单,谢谢!不过我遇到了一个问题,这似乎与我删除 (2) 位置中的幻灯片有关。我希望代码将幻灯片导出到第二个位置(标题之后的第一个),并且在我重新排列幻灯片之前,这往往可以正常工作:运行时错误演示文稿(未知成员):您选择打印的幻灯片没有不再存在。请进行另一个选择 当显然有一张幻灯片 (2) 时会发生这种情况,所以它可能与某些“内部”幻灯片索引或其他什么有关?任何想法或提示将不胜感激。
      猜你喜欢
      • 1970-01-01
      • 2021-03-26
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2012-09-15
      • 1970-01-01
      相关资源
      最近更新 更多