问题
I am trying to open .docx file and convert to PDF using Interop.Word.Application. It works as console application but If I use the same in my web application it doesn't work. I tried to see the permissions on the folder. I gave 'Network Service' with full control, but still I get object reference not set at word.Documents.Open. Could you please let me know what could be the problem? I am stuck with this error. Please let me know. I appreciate any suggestions. Thank you.
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object oMissing = System.Reflection.Missing.Value;
word.Visible = false;
word.ScreenUpdating = false;
string fileName = @"c:\OUTPUT\test.docx");
Document doc = word.Documents.Open(filename, 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);
doc.Activate();
回答1:
If you are still looking for the answer I have found it just a moment ago for my project.
Open DCOM Config Settings:
- Start ->
dcomcnfg.exe
- Computer
- Local Computer
- Config DCOM
- Search for
Microsoft Word 97-2003 Documents
-> Properties
Tab Identity
, change from Launching User
to Interactive User
回答2:
Try this it may help you.
- Create a new "Desktop" directory inside of "C:\Windows\SysWOW64\config\systemprofile\"
it works for me after a long long long day searching for the solution.
It seams to be a profile problem.
回答3:
This worked for me. https://social.msdn.microsoft.com/Forums/en-US/0f5448a7-72ed-4f16-8b87-922b71892e07/word-2007-documentsopen-returns-null-in-aspnet.
If this helpe you, please make it answer
回答4:
I did the following steps and it worked all right:
- Create the desktop folder
- Change the DCOM element to login with a specific user instead of an launching/interactive user
- Changed the service so it started as the same specific user
Edit on 2019-09-29. Due to a big windows update I had to do the exact progress all over again.
回答5:
As Paulie stated in his comment on OP, I think the issue is largely because of the libraries on the webserver. YOu might be able to hack to get this working on a web app, but I'd advise against it.
EDIT: Argh, I misread the post and just assumed Excel. That's what most people look for when using Office.Interop in my experience ;)
There is an open library that works with DocX formats (enter link description here). Unfortunately he admits on the page that conversions to formats like PDF is pretty much impossible without the Office.Interop libraries (http://cathalscorner.blogspot.com/2009/10/converting-docx-into-doc-pdf-html.html)
Here is a 'promising' alternative, but YMMV.
回答6:
It is most likely that you have a different word dll on your web server, perhaps an older one. I suggest that you reference a specific dll and copy it to the web server, so you can be sure that you are using the same word dll that you tested your code with and you know it works. There might be some problems when using interop, however, word interop is the only one which you can be sure will convert word files most correctly.
回答7:
To solve this I had to do both things:
Create the directories:
C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft \Windows\INetCache
C:\Windows\System32\config\systemprofile\Desktop
Enable DCOM setting as @gianni-b says.
If you're struggling to find the DCOM entry because of a different language, you can do this:
- Start Menu >
regedit
- Navigate to
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{00020906-0000-0000-C000-000000000046}
Right click > New > String value
- For Key, put
RunAs
- For Value, put
Interactive User
- For Key, put
You can do the same for Excel on AppID {00020812-0000-0000-C000-000000000046}
.
来源:https://stackoverflow.com/questions/10837437/interop-word-documents-open-is-null