问题
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 a test IIS server, it fails 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 loaded Word onto the server and set permission in DCom and got past that problem. But now I get this error on the line that starts with wRange = .....
As I said, the program works perfectly 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;
回答1:
- Click Start, click Programs, click Administrative Tools, click Component Services.
- Expand Component Services, expand Computers, expand My Computer, expand DCOM Config, and right-click Microsoft Word 97 - 2003 Document. Select Properties.
- Click General. Set Authentication Level to Connect (None also work).
- 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.
- Click button Apply.
- Click button OK.
For more information on “Configuring DCOM for Remote Access" visit Configuring DCOM for Remote Access
回答2:
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
回答3:
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.)
回答4:
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.
来源:https://stackoverflow.com/questions/5120104/getting-a-object-reference-error-in-prod-only-when-trying-to-use-the-word-docume