doc.Activate() set to an null reference in c# for Microsoft.Office.Interop.Word.Application word

77 Views Asked by At

i used Microsoft.Office.Interop.Word.Application word to convert word file to pdf file.This is perfectly working in my local iis.But when i add this to cloud server,It gives me a this error.

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

I already installed word in the cloud server.Word file and file path also correctly configured.

                        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                        object oMissing = System.Reflection.Missing.Value;
                        FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
                        word.Visible = false;
                        word.ScreenUpdating = false;
                        word.Visible = false;
                        word.ScreenUpdating = false;
                        foreach (FileInfo wordFile in wordFiles)
                        {
                            try
                            {
                                // Cast as Object for word Open method
                                Object filename = (Object)wordFile.FullName;
                                // Use the dummy value as a placeholder for optional arguments
                                Document doc = word.Documents.Open(ref filename, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                
                                doc.Activate();  --Error occured line
                                object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
                                object fileFormat = WdSaveFormat.wdFormatPDF;
                                // Save document into PDF Format
                                doc.SaveAs(ref outputFileName,
                                    ref fileFormat, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                                // Close the Word document, but leave the Word application open.
                                // doc has to be cast to type _Document so that it will find the
                                // correct Close method.                
                                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                                doc = null;
                            }
                            catch (Exception ex)
                            {
                                WriteToErrorLog("558", ex.ToString(),ex.Message);
                            }
                        }
                        // word has to be cast to type _Application so that it will find
                        // the correct Quit method.
                        ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
                        word = null;

Please advise me how to resove this and what are the things sould i have to double check in the server?

1

There are 1 best solutions below

0
On

You must grant access to start wordapp to iisuser to Office DCOM component on Control Panel - Administrative Tools - Component Services - DCOM Config - WordApplication - Properties - Security

using Word = Microsoft.Office.Interop.Word;

private string GenerateTempPdfFile(Stream stream, string newFileName = null)
        {
            var tempFileName = Path.Combine(Path.GetTempPath(), newFileName ?? Path.GetRandomFileName());
            var pdfFileName = tempFileName + ".pdf";
            if (File.Exists(pdfFileName))
            {
                return pdfFileName;
            }

            using (var fileWriter = new FileStream(tempFileName, FileMode.Create))
            {
                stream.CopyTo(fileWriter);
            }
            object fileName = tempFileName;
            object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
            lock (DocIo.locker)
            {
                var wordApplication = new Word.Application
                {
                    Visible = false,
                    ScreenUpdating = false
                };
                try
                {
                    var doc = wordApplication.Documents.Open(ref fileName);
                    try
                    {
                        object outputFileName = pdfFileName;
                        object fileFormat = Word.WdSaveFormat.wdFormatPDF;
                        doc.SaveAs(ref outputFileName, ref fileFormat);
                        return (string)outputFileName;
                    }
                    finally
                    {
                        doc.Close(ref saveChanges);
                    }
                }
                finally
                {
                    wordApplication.Quit();
                    File.Delete(tempFileName);
                }
            }
        }