title: C# Word document fails to open
body: <p>I have a website that opens a Word document on the user's browser, and fills in a certain value within the document:</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
   {
       string source = "c:\TemplateDocument.doc";
       string destination = "c:\Temp.doc";
        File.Copy(source, destination, true);
       Microsoft.Office.Interop.Word._Application oWord;
       Microsoft.Office.Interop.Word._Document oDoc;
       oWord = new Microsoft.Office.Interop.Word.Application();
       oDoc = oWord.Document.Open(destination, ReadOnly: false, Visible: false);
       oDoc.Tables[1].Rows[1].Cells[1].Range.Text = "Hello world"; // Simplified for the example
       oDoc.SaveAs2(destination);
       oDoc.Close();
       Response.Clear();
       Response.ContentType = "application/msword";
       Response.AddHeader("Content-Disposition", "inline;TemplateDocument.doc");
       Response.WriteFile(destination);
       Response.End();
       File.Delete(destination);
  
    }
  </code></pre>
  
  <p>The above code worked perfectly on our <em>old</em> server, which was IIS6.</p>
  
  <p>However, we upgraded the server to IIS7, and made it work with DFS.</p>
  
  <p>(DFS - basically, two replicating servers)</p>
  
  <p>What happens now is that <code>oDoc</code> remains null, even after the <code>Open()</code> command.</p>
  
  <p>The moment I try accessing anything within <code>oDoc</code> (such as <code>oDoc.Tables</code>), I get a <code>NullReferenceException</code>.</p>
  
  <p>When running the code <strong>locally</strong> on the server, it works; once it's published, I get the exception.</p>
  
  <p>What else might be of relevance? Code refers to the dll of Office 2010, which is the Office installed on the server (this was true also before the upgrade, so I don't think that's what is causing the problem).</p>
  
  <p>Thanks in advance!</p>