1

This below is the code i have. If i comment out the block from object fName to adoc.Close the code works on IIS but if it is not commented out it gives me this error

NullReferenceException: Object reference not set to an instance of an object.] QuoteProject030117.Controllers.Quote1Controller.Create(Quote1 quote1) +1653

I think iis is having a problem modifying the word doc but i cant find a way to fix it. All of the code works perfectly on Visual Studio when i run it. If anyone could give me a hand would be appreciated. Thank you!

public ActionResult Create([Bind(Include = "id,quotenumber,date,value,won,lost,client,projectdescription,contact")] Quote1 quote1)
    {
        if (ModelState.IsValid)
        {
            db.Quote1.Add(quote1);
            db.SaveChanges();

            string QNsql = @"SELECT quotenumber FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
            string quoteNum = db.Database.SqlQuery<string>(QNsql).Single();
            string Dsql = @"SELECT date FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
            string date = db.Database.SqlQuery<string>(Dsql).Single();
            string Clientsql = @"SELECT client FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
            string client = db.Database.SqlQuery<string>(Clientsql).Single();
            string Contsql = @"SELECT contact FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
            string contact = db.Database.SqlQuery<string>(Contsql).Single();
            string PDessql = @"SELECT projectdescription FROM Quote1 ORDER BY id DESC offset 0 rows fetch next 1 rows only;";
            string proDes = db.Database.SqlQuery<string>(PDessql).Single();

            System.IO.Directory.CreateDirectory(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum);

            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Information Recieved From Customer"));
            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "PO Recieved From Customer"));
            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Project Costing Sheet"));
            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Quotation Sent To Customer"));
            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(@"C:\Users\alanf\Documents\CopyTo\" + quoteNum, "Quotations Recieved From Vendors"));

            string fileName = "Quote.docx";
            string sourcePath = @"C:\inetpub\wwwroot";
            string targetPath = @"C:\Users\alanf\Documents\CopyTo\" + quoteNum + @"\Quotation Sent To Customer";

            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            System.IO.File.Copy(sourceFile, destFile, true);


            object fName = System.IO.Path.Combine(targetPath, fileName);
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false };
            Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fName, ReadOnly: false, Visible: false);
            aDoc.Activate();
            FindAndReplace(wordApp, "<quote>", "" + quoteNum + "");

            FindAndReplace(wordApp, "<date>", "" + date + "");

            FindAndReplace(wordApp, "<client>", "" + client + "");

            FindAndReplace(wordApp, "<contact>", "" + contact + "");

            FindAndReplace(wordApp, "<projectdescription>", "" + proDes + "");
            aDoc.Close();

            Process.Start("explorer.exe", @"C:\Users\alanf\Documents\CopyTo\" + quoteNum);

            return RedirectToAction("Index");
        }
Rufus L
  • 36,127
  • 5
  • 30
  • 43

1 Answers1

1

Office interop from IIS is not supported by Microsoft - see https://social.msdn.microsoft.com/Forums/en-US/1d3923a5-6720-4743-8fa0-c919ff90e4ef/i-cant-open-word-office-on-iis7?forum=worddev and https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office

It would be good to know what line is 1653 as @OmegaMan asked too.

For your scenario, I would recommend looking at a .NET library that supports reading and writing Word documents or doing mail merge. I have used Aspose before, but there are others out there too like GemBox (I haven't used them though).

Also, see this thread Reading doc and docx files using C# without having MS Office installed on server

Mike Dearman
  • 316
  • 2
  • 5