将PPT文件转换为PDF

时间:2017-09-18 09:16:07

标签: pdf-generation powerpoint

我有一个宏打开存储在工作簿上的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

Sub 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

End Sub

1 个答案:

答案 0 :(得分:0)

我删除了一些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