Getting object reference error in prod only when trying to use Word DocumentClass

I am writing a program that uses the .dotx template and performs data merging on an aspx page. The program works fine on my Dev workstation locally, but when I deploy it on the IIS test server, it does not work on the second line below, giving me an object reference error.

I ran into problems earlier because the Word Com object was not on the IIS server, so I uploaded Word to the server and set the resolution to DCom and overcame this problem. But now I get this error in the line starting with wRange = .....

As I said, the program works fine locally in debug mode.

Any ideas?

Microsoft.Office.Interop.Word.DocumentClass System.NullReferenceException: Object reference not set to an instance of an object 

lines of code:

 Document BaseDocument = oWord.Documents.Open(ref oTemplate, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); wRange = BaseDocument.Bookmarks.get_Item(ref endOfDoc).Range; 
+6
c # ms-word office-automation
source share
4 answers
  • Click "Start", select "Programs", "Administrative Tools", "Component Services."
  • Expand Component Services, Computers, My Computer, DCOM Configuration, and right-click Microsoft Word 97 - 2003 Document . Select "Properties."
  • Click "General." Set the authentication level for the connection (also do not work).
  • Click Identity. Install This User . Specify the user account that will always be used to start the COM application, regardless of which user accesses it.
  • Click the "Apply" button.
  • Click OK.

For more information about "Configuring DCOM for Remote Access," visit Configuring DCOM for Remote Access.

+3
source share

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

0
source share

After installing Office on the target machine, make sure that you open one of the applications, such as Word, because there may be more license activation requests. The API will not work until you go through this process and the errors that occur are not clear as to what the problem really is.

Thus, a NullReferenceException may be caused by the fact that Word cannot open the document, thereby Word.Documents.Open () returns null. Honestly, I'm not 100% sure, this is what causes your problem. As I said, errors will not clearly tell you what the problem is. (I accidentally stumbled upon this scenario a while ago, and although I know that the API throws an exception, which I cannot remember if it matches what you see.)

0
source share

Your program does not work locally in debug mode. You just did not use any errors.

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 to appeal to an interactive desktop application. Many of the assumptions they make do not apply to server applications.

For example, a desktop application synchronizes when user actions become messages in the Windows message queue. ASP.NET does not have a Windows message queue, so access to shared data is not synchronized. Your tests may simply never cause multiple threads to run simultaneously.

There may be data that exists in only one copy for all users of this COM component. This works great in a desktop application as there is only one user. Your ASP.NET application has many users who execute code at the same time. Another assumption is violated.

All these things will create subtle mistakes that really cannot be fixed. They can only be moved, leaving you to think that you have solved the problem, up to the moment when you understand that there is another problem. This cycle never ends and never can end, because you will not solve the actual problem: the problem was that you used the Office Automation API from the server application.

Fix this problem and you will no longer have such errors.


PS It also happens that you may violate your license for Microsoft Word, unless all users of your ASP.NET application have licenses for the Word application.

-3
source share

All Articles