COMException while exporting as PDF
- Dear friends,
I am getting this exception in code while trying to save the document as PDF. I even have the "Save as PDF or XPS" plugin installed.
"System.Runtime.InteropServices.COMException (0x80004005): The export failed due to an unexpected error.
at Microsoft.Office.Interop.Word.DocumentClass.ExportAsFixedFormat(String OutputFileName, WdExportFormat ExportFormat, Boolean OpenAfterExport, WdExportOptimizeFor OptimizeFor, WdExportRange Range, Int32 From, Int32 To, WdExportItem Item, Boolean IncludeDocProps, Boolean KeepIRM, WdExportCreateBookmarks CreateBookmarks, Boolean DocStructureTags, Boolean BitmapMissingFonts, Boolean UseISO19005_1, Object& FixedFormatExtClassPtr)"
This also causes issue while i try to add the 'heading' styles in to my Table of contents. Following is my error trace
"System.Runtime.InteropServices.COMException (0x800A14CA): There is insufficient memory or disk space. Word cannot display the requested font."
Thanks in advance.
全部回复
- Hi Karthik
I recommend you post a separate message thread for the second problem. I rather doubt the two issues are related.
No one can help you if you don't provide the code that causes the errors. You also need to specify the version of the Office application you're working with. Yes, we could make an educated guess, but assumptions are often dangerous as the problem could be due to our assumption (the version) being wrong.
Cindy Meister, VSTO/Word MVP - Hi Cindy,
Glad to see your reply. The below is my piece of code to export the output document as PDF
"wordDoc.Application.ActiveDocument.ExportAsFixedFormat(filePath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF,
false, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent,
true, true, Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks,
true, false, false, ref _missing);"
Version of office /Word : 2007 - I am having a similar problem. I am using MS Word 2007 and have installed the "Microsoft Save as PDF Add-in for 2007 Microsoft Office Program".
I recorded a macro to save the current document to PDF. It was recorded as follows (with changes in paths and file name for privacy sake):
Sub DocToPdf()
'
' DocToPdf Macro
'
'
ChangeFileOpenDirectory _
"C:\temp\"
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:\temp\how_to.pdf" _
, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=False, _
BitmapMissingFonts:=False, UseISO19005_1:=False
End Sub
When I recorded this macro it successfully wrote the PDF version of the file. When I then tried to execute the macro using "C:\PROGRA~1\MICROS~4\Office12\WINWORD.EXE C:\temp\how_to.docx /mDocToPdf" it failed and I was presented with a dialog box showing a message stating "Run-time error '-2147467259 (80004005)': The export failed due to an unexpected error.".
I thought perhaps it was because the PDF file already existed and tried deleting the PDF file and re-running the command. But this didn't improve the situation.
I also find similar results if I use a script to access the MS Word COM object and try to invoke the ExportAsFixedFormat method that way.
I hope you can provide a solution because I really need this to work.
Steve - Hi Karthik
I apologize for the delayed response. I've been very busy with a project and had to wait until I could find a block of time I could devote to testing...
The following code works fine for me. Please note that the try...catch block was required in order for this to work:
Please also note that care must be taken with the file name: it must have the correct extension (PDF).try { wdApp.ActiveDocument.ExportAsFixedFormat(@"C:\Test\TestSaveAsPDF_4.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, false, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, true, true, Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, false, false, ref missing); } catch (Exception ex) { MessageBox.Show(ex.Message); }
I suspect that the problem (needing to be in try...catch) may have something to do with the fact that the engine that does the actual saving is not built into Word, but an add-on package, and calling it from code out-of-process, as opposed to in-process VBA (my VBA tests worked fine FWIW).
Cindy Meister, VSTO/Word MVP - Hi Cindy,
Thanks for your code. Let me try that out. - Cindy, the parameters which i passed is also same as what you have given. correct? the only change is that i need to catch the exception.
- Hi Karthik
Yes, I performed two tests, one with a random set of parameter values, one with the same set of parameter values you used (with the exception of the file name, which you don't show us). Both worked the same: the file was saved successfully and no errors. Based on that, the only thing I can see is that you should try putting it in a try...catch block.
Cindy Meister, VSTO/Word MVP - Cindy i have one more quick question, I performed my application test in an other machine which has windows 2003 server and word 2007. I installed the necessary fonts which i refer in my application. I got my desired output in the document but in the exported PDF i lost all my fonts and the content used some default fonts. Even i tried manually saving the output doc as PDF and it worked. i am not sure why it lost the font while saved as PDF programmatic. Please advice.
- I determined that the problem I was having was specific to one computer. I uninstalled Foxit Reader and installed Adobe Reader 9 and rebooted and now I am no longer having a problem automating MS Word 2007 to write documents out in PDF.
Steve

