Programmatically convert Power Point presentation to PDF in .net

0

After searching a bit I still haven't got what I need. I'm trying to convert various files to PDF using VB.Net and referencing various MS Office components like Word/Excel/PowerPoint as either COM objects or by using the PIA (Office Interop Assemblies). In the end I want to use the COM method because it is version independent and that's important.

For Word and Excel I can make it work both ways. But for PowerPoint I'm running into problems and would appreciate some suggestions.

Here's the 2 methods

COM

Dim appPP As Object = CreateObject("PowerPoint.Application")
Dim docPP As Object = appPP.Presentations.Open(strAttachmentFileName)
'2 is Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF
docPP.ExportAsFixedFormat(strNewFileName, 2)
docPP.Close()
appPP.Quit()

This method gets an error at the Export line - Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

PIA - this works ok

Imports Microsoft.Office.Interop
' PowerPoint requires this, to add a reference use COM - MS Office Type Library of same version as interop
Imports Microsoft.Office.Core 
Dim appPP As New PowerPoint.Application
Dim docPP As PowerPoint.Presentation = appPP.Presentations.Open(strAttachmentFileName)
docPP.ExportAsFixedFormat(strNewFileName, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF)
docPP.Close()
appPP.Quit()

EDIT

Also in the COM version I tried the fully qualified version including references to the PIA and Office Core like this

docPP.ExportAsFixedFormat(strNewFileName, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF, PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentScreen, MsoTriState.msoFalse, PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PowerPoint.PpPrintOutputType.ppPrintOutputSlides, MsoTriState.msoFalse, Nothing, PowerPoint.PpPrintRangeType.ppPrintAll, "", False, False, False, False, False)

And it still gets the error

.net
vb.net
pdf
ms-office
powerpoint
asked on Stack Overflow Mar 11, 2016 by nuander • edited Mar 11, 2016 by nuander

1 Answer

1

After experimenting with the COM version of the ExportAsFixedFormat I think there is a bug in the COM object. I did find a workable solution tho

docPP.SaveAs(strNewFileName, 32)

I'm not sure what the version compatibility is on that. I have Office 2010.

answered on Stack Overflow Mar 11, 2016 by nuander

User contributions licensed under CC BY-SA 3.0