Search code examples
c#asp.netoffice-interop

.NET Website Publish Website on Web Server Activating Word Documents


Aim of the website is for a user to enter values into input fields and these values are used to find and replace text in word documents.

This works locally on my machine and replaces word documents and save them in the correct path.

I tried to publish the website using File System to a location on a web server. I created a new site on IIS manager and this is currently working fine, the website page loads. As soon I click on a button to generate the word documents I get the following error:

[NullReferenceException: Object reference not set to an instance of an object.] src_PoA.OpenWordFile(String doc) +658
src_PoA.Submit_Click(Object sender, EventArgs e) +569
System.Web.UI.HtmlControls.HtmlButton.OnServerClick(EventArgs e) +133 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1664

It seems to be failing when trying to define the word application, a word document and attempting to active the word document. I tried to debug it by opening the IIS website using visual studio on the web server, however I keep getting Unable to start debugging on the web server.:

 private void OpenWordFile(string doc)
    {
        string caseno = casetextboxinput.Value;
        string nameno = nametextbox.Value;
        object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, doc);
        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
        Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(doc, ReadOnly: false, Visible: true);
        aDoc.Activate();
}

I am using the UNC path in visual studio and this seems to find the path ok locally, but i dont know if it actually is able to access/read and write files back to this location which could be another place its also going wrong.

private static string directoryroot = @"\\testserver05\c$\inetpub\wwwroot\PoA\PoA Testing"; 

I have got .NET 4.5 and the bundle 4.0, 3 and 2 all installed on the web server. Also microsoft word is also installed.


Solution

  • Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    As a workaround you may consider using the Open XML SDK, see Welcome to the Open XML SDK 2.5 for Office for more information. Or just consider using any third-party components designed for the server-side execution.