none
“Command Failed” while trying to convert Word documents to PDF RRS feed

  • Question

  • I have a function in my code that converts Word documents to PDF. The code works perfectly fine in converting doc,docx file to PDF . However for certain word documents, the application is throwing an exception "Command Failed".

    For one of the files that was producing this error, I tried modifying the existing settings of the file by navigating to File > Help > Options > Trust Center > Trust Center Settings > File Block Settings but it didn't work.

    I am using the below code for conversion


     Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                //Opening the Word document that is temporarily created
                Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(tempPath);
                // VERY IMPORTANT: do this to close the MS Word instance
                doc.SaveAs(pdfFilePath, WdSaveFormat.wdFormatPDF);
                ((Microsoft.Office.Interop.Word._Document)doc).Close();
                ((Microsoft.Office.Interop.Word._Application)app).Quit();
                //Additional code to close Word Document and release resources after opening
                if (app != null)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                if (doc != null)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);

    "tempPath" is location of the word document and "pdfFilepath" is the location where the pdf document is to be saved.

    Any suggestions ?

    Thursday, September 7, 2017 10:43 AM

All replies

  • Hi anz1991,

    I failed to reproduce your issue. If you tried to save as these word documents as pdf file manually, will you get the error message? Could you share some documents with issue so that we could try to reproduce your issue. You could share documents with One Drive and put link here.

    Best Regards,

    Terry

    Friday, September 8, 2017 1:38 AM