Error Object reference not set to an instance of an object. Microsoft.Office.Interop.Word.Application

0

I am developing a code to convert a word document into pdf. When I run the application through Visual Studio, the conversion is performed and no error is displayed. When I run the application on the server where IIS is installed, the document is not converted and an error is returned.

Code:

            // Abrir Aplicacao Word
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

            string WordFile = @"c:\Projetos\Conversoes\Word.docx";

            object filename = (object)WordFile;

            // Arquivo de Destino
            object newFileName = @"c:\Projetos\Conversoes\ArquivoConvertido.pdf";

            object missing = System.Reflection.Missing.Value;

            object onlyread = false;

            // Abrir documento
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref filename, ref missing, onlyread, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);

            // Formato para Salvar o Arquivo – Destino  – No caso, PDF
            object formatoArquivo = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

            // Salvar Arquivo
            doc.SaveAs(ref newFileName, ref formatoArquivo, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            // Não salvar alterações no arquivo original
            object salvarAlteracoesArqOriginal = false;
            wordApp.Quit(ref salvarAlteracoesArqOriginal, ref missing, ref missing);

            Marshal.ReleaseComObject(wordApp);

Error:

Message:

[1] Exception of type 'System.Web.HttpUnhandledException' was thrown.

[2] Object reference not set to an instance of an object.

Event:

[1] System.Web.HttpUnhandledException

[2] System.NullReferenceException

Location:

[1] Boolean HandleError (System.Exception)

[2] Void Page_Load (System.Object, System.EventArgs)

Source:

[1] System.Web

[2] GestHos

Stack:

[1] at System.Web.UI.Page.HandleError (Exception e) at System.Web.UI.Page.ProcessRequestMain (Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest (Boolean includeStagesBeforeAsyncPoint , Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest () at System.Web.UI.Page.ProcessRequest (HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute () at System.Web.HttpApplication.ExecuteStep (IExecutionStep step, Boolean & completedSynchronously)

[2] at Document.Converser.Convert.Page_Load (Object sender, EventArgs, and) at System.Web.UI.Control.LoadRecursive () at System.Web.UI.Page.ProcessRequestMain (Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    
asked by anonymous 20.01.2016 / 17:32

0 answers