none
Office.Interop.Word conversion failure RRS feed

  • Question

  • Hi guys, I am currently stumped I have an API that I put together to take a file that gets posted to it in DOCX format and have it return a PDF, on my local machine it works without any problems, it creates the file on the file system in both scenarios but on my local test server it does not return the PDF, while my local machine does.

    My server has Office 2013 installed, and so does my system. I wrote it to take files in a folder and convert them one by one, that works fine on the server, but I have no idea why it failing right now.

            public string ConvertFile(string FileName)
            {
                Word.Application W = new Word.Application();
                try
                {
                    object misValue = System.Reflection.Missing.Value;
                    
                    W.Visible = false;                
                    W.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
    
                    object varMissing = Type.Missing;
    
                    Word.Document D = W.Documents.Open(FileName, ReadOnly: true);
    
                    string pdfFilename = FileName;
                    string destination = pdfFilename.Substring(0, pdfFilename.LastIndexOf('.')) + ".PDF";
    
                    D.ExportAsFixedFormat(destination, Word.WdExportFormat.wdExportFormatPDF);
                    D.Close(Word.WdSaveOptions.wdDoNotSaveChanges);               
    
                    return destination;
                }
                catch (Exception ex)
                {
                    return ex.InnerException.ToString();
                }
                finally
                {
                    W.Quit();
                }
            }

    Am I missing something?

    With SOAPUI on my local machine it returns what I am looking for but from the local server it merely returns a hardly helpful message.

    HTTP/1.1 500 Internal Server Error
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/json; charset=utf-8
    Expires: -1
    Server: Microsoft-IIS/10.0
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Date: Wed, 21 Sep 2016 12:00:46 GMT
    Content-Length: 36

    {"Message":"An error has occurred."}

    • Moved by CoolDadTx Wednesday, September 21, 2016 3:42 PM Office related
    Wednesday, September 21, 2016 12:08 PM

Answers

All replies

  • Not sure what type of project this is but it looks related to ASP.NET. I would recommend re-posting your question to one of those forums:

    http://forums.asp.net/


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, September 21, 2016 12:27 PM
  • Hello,

     Are you saving the DOCX converted to PDF on the Server or Client?

    Thanks :)

    Wednesday, September 21, 2016 12:34 PM
  • Will do, thank you.

    Wednesday, September 21, 2016 12:45 PM
  • I save the DOCX file on the server (all good), and then convert the file on the server to PDF but that is where it seems to fail. Once the conversion starts it is like it is failing to open Word by the looks of it, which in turn is failing to convert.
    Wednesday, September 21, 2016 12:49 PM
  • I save the DOCX file on the server (all good), and then convert the file on the server to PDF but that is where it seems to fail. Once the conversion starts it is like it is failing to open Word by the looks of it, which in turn is failing to convert.

    Keep in mind that the server-side automation of Office applications is not really supported  because of the limited threading model of the apps.

    https://support.microsoft.com/en-us/kb/257757


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by David_JunFeng Friday, September 30, 2016 2:12 AM
    • Marked as answer by David_JunFeng Friday, September 30, 2016 2:12 AM
    Wednesday, September 21, 2016 1:00 PM
  • I doubt the load will be much, this is more for convenience than anything else. I do see there what you mean with not doing it like that though.


    Is there something else you can recommend?

    Wednesday, September 21, 2016 1:04 PM
  • Hi Heinz09,

    According to your description, you could refer to Document Creation and Conversion with the OpenXML SDK and SharePoint 2010 Word Automation Services

    Thanks for your understanding.
    • Proposed as answer by David_JunFeng Friday, September 30, 2016 2:12 AM
    • Marked as answer by David_JunFeng Friday, September 30, 2016 2:12 AM
    Thursday, September 22, 2016 2:27 AM
  • You may try some other way to convert Word file to PDF on server. It doesn't require MS Word to be installed.

    Convert Word document to PDF

    Friday, September 30, 2016 7:23 AM
  • I found something that is working quite well. Unfortunately the company is not looking at SharePoint, so we opted to go for GemBox software Document converter, it is working quite well with no issues at the moment, but we are still testing should probably know int he next few days.

    Currently it is working for the windows service, I am now integrating it to Web API.

    Thank you for pointing me in the right direction.

    Friday, September 30, 2016 7:28 AM