title: Automating Word Document Processing with ASP.Net
body: <p>I am trying to create a process, in my Asp.net application, that will open up a Microsoft Word template (stored on the server) and edit specific fields in the document then download the file to the users desktop.  This works fine on my local server but when pushed to our production server, it fails (no errors or warnings). Here is the code I have:</p>
  
  <pre><code>location = mappedPath &amp; ConfigurationManager.AppSettings("defaultTemplate")
  Dim objWordApp As New Microsoft.Office.Interop.Word.Application
  objWordApp.Visible = False
  Dim objDoc As Word.Document = objWordApp.Documents.Open(location)
  objDoc.Activate() 
  objDoc.Content.Find.Execute(FindText:="[SomeField]", ReplaceWith:=myField,    Replace:=Word.WdReplace.wdReplaceAll)
  objDoc.Content.Find.Execute(FindText:="[someId]", ReplaceWith:=myId,    Replace:=Word.WdReplace.wdReplaceAll)
  outputLocation = mappedPath &amp; "Temp/Posting_" &amp; Now.ToString("MMddyyyy_hhmmss") &amp; ".docx"
  objDoc.SaveAs(outputLocation)
  
  objDoc.Close(Word.wdSaveOptions.wdDoNotSaveChanges)
  objDoc = Nothing
  objWordApp.Quit(Word.wdSaveOptions.wdDoNotSaveChanges)
  objWordApp = Nothing 
  </code></pre>
  
  <p>The code then goes on to send the outputlocation to the response.Write buffer, like this:</p>
  
  <pre><code>  Dim fi As FileInfo = New FileInfo(outputLocation)
    If fi.Exists Then
       Response.Clear()
       Response.ClearHeaders()
       Response.ClearContent()
       Response.Charset = "UTF-8"
       Response.ContentEncoding = System.Text.Encoding.UTF8
       Response.AddHeader("content-disposition", "attachment; filename=" + Server.UrlEncode(fi.Name))
       Response.AddHeader("Content-Type", "application/msword")
       Response.ContentType = "application/msword"
       Response.AddHeader("Content-Length", fi.Length.ToString())
       Response.WriteFile(fi.FullName)
       HttpContext.Current.ApplicationInstance.CompleteRequest()
   Else
      'log it
   End If
  </code></pre>
  
  <p>This code fails, on the server, at this line:</p>
  
  <pre><code>Dim objDoc As Word.Document = objWordApp.Documents.Open(location)
  </code></pre>
  
  <p>From what I have read, Microsoft does not support the automation of Office products on a server side.  That being said, is there a way to fix this or is there an alternative?  I have verified that the server that is running the app has Word installed and the user has the correct permissions.</p>
  
  <p>As I stated, I need to be able to read/edit a file on the server and download the file to the user.  If anyone has any suggestions, please let me know.</p>
  
  <p>thanks</p>