将 PPT 文件转换为 PDF

Converting PPT file to PDF

我有一个宏可以打开存储在工作簿上的 PowerPoint 文件,然后使用以下代码对其进行修改

Set PApp = CreateObject("PowerPoint.Application")
PApp.Visible = True
Pth = ThisWorkbook.Path
ErrorPopUp = True

Dim TsyTemplate As Object

Set TsyTemplate = ThisWorkbook.Sheets("Report    Templates").OLEObjects(“Report 1”)
TsyTemplate.Copy
Sheets("Version Control").Paste
Set TsyTemplate = ThisWorkbook.Sheets("Book 1").OLEObjects(1)
TsyTemplate.Verb Verb:=xlOpen

Set TsyTemplate = Nothing

Set PPres = PApp.ActivePresentation

这工作正常,但我想添加一些代码,然后将打开的 PowerPoint 文件转换为 PDF 文件。我更希望它只是转换它而不将它保存在某个地方但我不相信这是可能的所以我使用他下面的代码将它保存为 PDF 文件

PDFName = ActiveWorkbook.Path & "/test.pdf"
PPres.ExportAsFixedFormat Path:=PDFName,     FixedFormatType:=ppFixedFormatTypePDF,     RangeType:=ppPrintSelection

这不起作用,因为我收到错误消息 "type mismatch"

有没有人对我做错了什么有什么建议。

谢谢

完整代码:

Global PApp As Object
Global PPres As Object
Global PPTFileName As String
Global ppFixedFormatTypePDF As Long
Global ppPrintSelection As Long

子Test_Printing_To_PDF()

Set PApp = CreateObject("PowerPoint.Application")
PApp.Visible = True
Pth = ThisWorkbook.Path
ErrorPopUp = True

Dim TsyTemplate As Object

Set TsyTemplate = ThisWorkbook.Sheets("Report Templates").OLEObjects("Report 1")
TsyTemplate.Copy
Sheets("Version Control").Paste

Set TsyTemplate = ThisWorkbook.Sheets("Version Control").OLEObjects(1)

TsyTemplate.Verb Verb:=xlOpen

Set TsyTemplate = Nothing

Set PPres = PApp.ActivePresentation


PPres.Slides(1).Shapes("Presentation_Title").TextFrame.TextRange.Text = "Test printing code"


ppFixedFormatTypePDF = 2
ppPrintSelection = 2


PDFName = ActiveWorkbook.Path & "/test.pdf"
PPres.ExportAsFixedFormat Path:=PDFName, FixedFormatType:=ppFixedFormatTypePDF, RangeType:=ppPrintSelection

结束子

我删除了您的一些 Excel 代码,以便我可以在这里尝试;因为它似乎与从 PPT 导出 PDF 无关,所以应该没有任何区别。下面带有注释的新(工作)代码:

Option Explicit

Global PApp As Object
Global PPres As Object
Global PPTFileName As String
Global ppFixedFormatTypePDF As Long
Global ppPrintSelection As Long

Const ppSaveAsPDF As Long = 32

Sub Test_Printing_To_PDF()

' Always include Option Explicit and DIM all variables
Dim Pth As String
Dim ErrorPopUp As Boolean
Dim PDFName As String

Set PApp = CreateObject("PowerPoint.Application")
PApp.Visible = True
Pth = ThisWorkbook.Path
ErrorPopUp = True

' Just invoking PowerPoint doesn't necessarily create a presentation.
' You need to add one (or open an existing one)
Set PPres = PApp.presentations.Add

' And creating a new presentation doesn't necessarily add slides so:
PPres.slides.Add 1, 1

' Unless you've opened a presentation that happens to have a shape named
' Presentation_Title on the first slide, this will fail:
'PPres.slides(1).Shapes("Presentation_Title").TextFrame.TextRange.Text = "Test printing code"
' So I've changed it to this:
PPres.slides(1).Shapes(1).TextFrame.TextRange.Text = "Test printing code"

' / isn't a valid character:
'PDFName = ActiveWorkbook.Path & "/test.pdf"
' so I changed it to this:
PDFName = ActiveWorkbook.Path & "\test.pdf"

' And there are all sorts of reports all over the net about
' the Export routine being buggy.  Substitute this and it works:
PPres.SaveAs PDFName, ppSaveAsPDF


End Sub