Search code examples
vb.netvbaoffice-interopexcel-interop

Save file as PDF/A using Office.Interop.Excel


How can I export an Excel spreadsheet to PDF/A (ISO 19005-1)?

EDIT: I'm asking for PDF/A, and not plain old PDF 1.5 as it exports by default. I've even emphasized the A in my original question.

I can already export Word and PowerPoint documents to PDF/A, using the ExportAsFixedFormat() function, since the Word and PowerPoint functions both have an optional UseISO19005_1 parameter, but the Excel version is very different, and is missing lots of parameters.

I can't seem to find any way to export a PDF/A using the COM Interop.

Here's the code I use to export from a docx:

Dim ExportFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
Dim OpenAfterExport As Boolean = False
Dim OptimizeFor As WdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint
Dim Range As WdExportRange = WdExportRange.wdExportAllDocument
Dim Item As WdExportItem = WdExportItem.wdExportDocumentWithMarkup
Dim IncludeDocProps As Boolean = True
Dim KeepIRM As Boolean = False
Dim CreateBookmarks As WdExportCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks
Dim DocStructureTags As Boolean = True
Dim BitmapMissingFonts As Boolean = True
Dim UseISO19005_1 As Boolean = False

If exportPDFA Then
    UseISO19005_1 = True
    Dim wordApp As New Word.Application()
    Dim doc As Word.Document = wordApp.Documents.Open(FileName)
    doc.ExportAsFixedFormat(pathToDestFile, ExportFormat, OpenAfterExport, OptimizeFor, Range, 0, 0, Item, IncludeDocProps, KeepIRM, CreateBookmarks, DocStructureTags, BitmapMissingFonts, UseISO19005_1)
End If

But for xlsx, the ExportAsFixedFormat() function accepts very different parameters (this was taken directly from the Microsoft.Office.Interop.Excel class):

Sub ExportAsFixedFormat(Type As XlFixedFormatType, Optional Filename As Object = Nothing, Optional Quality As Object = Nothing, Optional IncludeDocProperties As Object = Nothing, Optional IgnorePrintAreas As Object = Nothing, Optional From As Object = Nothing, Optional [To] As Object = Nothing, Optional OpenAfterPublish As Object = Nothing, Optional FixedFormatExtClassPtr As Object = Nothing)

Solution

  • Excel allows the user to choose whether or not to save the file as PDF/A in the options of the save dialog. This setting is stored in the Registry as LastISO19005-1 (REG_DWORD) under HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\FixedFormat. (Replace 12.0 with the right version.) The ExportAsFixedFormat function also respects this setting. You can set this value to 1 before calling ExportAsFixedFormat to get Excel to export the file as PDF/A.

    I know this solution is not pretty as it uses global state to address a local problem. Consider restoring the previous value when you're done.