Getting a Object Reference error in prod only when trying to use the Word DocumentClass

前端 未结 4 1513
醉话见心
醉话见心 2021-01-05 10:31

I am writing a program that uses a .dotx template and does a merge of data in an aspx page. The program works perfect on my Dev workstation locally but when I deploy it to

相关标签:
4条回答
  • 2021-01-05 10:46

    If word is really installed on the server check if the User account where IIS is running under has actually permission on the Microsoft.Office.Interop Assembly

    0 讨论(0)
  • 2021-01-05 10:48

    Your program does not work locally in debug mode. You simply haven't exercised any of the bugs.

    You should never, under any circumstances, use any of the Office Automation APIs from an ASP.NET application or any other server application. These APIs are designed as calls to an interactive, desktop application. Many of the assumptions they make do not apply to server applications.

    For instance, a desktop application is synchronized by user actions becoming messages in the Windows message queue. There is no Windows message queue in your ASP.NET application, so access to shared data is not synchronized. Your tests may simply never have caused multiple threads to run at the same time.

    There may be data that exists in only a single copy for all users of a given COM component. This works fine in a desktop application, since there is only a single user. Your ASP.NET application has many users all executing code at the same time. Another assumption violated.

    All of these things will produce subtle bugs which cannot actually be fixed. They can only be moved around, leaving you to think you've solved the problem, right up to the time you realize that there's one more problem. This cycle never ends and never can end, because you will not have solved the actual problem: the problem was that you were using an Office Automation API from a server application.

    Fix that problem, and you'll have no more bugs of this kind.


    P.S. It also just so happens that you may be violating your license for Microsoft Word, unless all of the users of your ASP.NET application have licenses for the Word application.

    0 讨论(0)
  • 2021-01-05 10:52

    After installing Office on the target machine, make sure you open one of the apps like Word directly because there may be more prompts for activating the license. The API won't work until you step through this process and the errors generated will not be clear as to what the problem really is.

    The NullReferenceException may thus be caused by Word being unable to open the document, thereby Word.Documents.Open() would return null. To be honest I am not 100% sure this is what is causing your problem -- As I stated, the errors will not clearly tell you what the problem is. (I ran into this scenario myself a while ago and though I know the API was throwing an exception I cannot remember if it is the same as what you're seeing.)

    0 讨论(0)
  • 2021-01-05 11:01
    1. Click Start, click Programs, click Administrative Tools, click Component Services.
    2. Expand Component Services, expand Computers, expand My Computer, expand DCOM Config, and right-click Microsoft Word 97 - 2003 Document. Select Properties.
    3. Click General. Set Authentication Level to Connect (None also work).
    4. Click Identity. Set This user. Specify a user account that will always be used to run the COM application regardless of which user is accessing it.
    5. Click button Apply.
    6. Click button OK.

    For more information on “Configuring DCOM for Remote Access" visit Configuring DCOM for Remote Access

    0 讨论(0)
提交回复
热议问题