24

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();
Berkay Turancı
  • 3,373
  • 4
  • 32
  • 45
nav100
  • 2,923
  • 19
  • 51
  • 89
  • Sorry, I can't help you exactly but I would heavily advise against using Office Interop in a web app, MS advise against it and as far as I am aware office is not licensed to be used on a web server. Apologies if that is irrelevant to you, there are a lot of third party components which would perform this for you but I've never used an open source one. – Paulie Waulie May 31 '12 at 16:22
  • Is there anything in the event log? – Paulie Waulie May 31 '12 at 16:27
  • 2
    There's a bug in that code. string fileName = @"c:\OUTPUT\test.docx"); <--- has a trailing `)`. please post the actual code that's in your application. – Eoin Campbell May 31 '12 at 16:35
  • There is nothing in the event log. – nav100 May 31 '12 at 16:35
  • answer with creating Desktop folder helped for me: http://stackoverflow.com/questions/1006923/automating-office-via-windows-service-on-server-2008 – zchpit Jul 14 '16 at 15:02

7 Answers7

73

If you are still looking for the answer I have found it just a moment ago for my project.

Open DCOM Config Settings:

  1. Start -> dcomcnfg.exe
  2. Computer
  3. Local Computer
  4. Config DCOM
  5. Search for Microsoft Word 97-2003 Documents -> Properties

Tab Identity, change from Launching User to Interactive User

Gianni B.
  • 2,691
  • 18
  • 31
  • 8
    Please note on Windows 64bit OS, you must start MMC in 32bit mode MMC -32 and then add in the Component Services. Otherwisem you only see 64 bit components. See IT with Schprit http://schprit.blogspot.com/2010/11/using-microsoft-office-2007-with-dcom.html – Gary Kindel Nov 07 '12 at 16:30
  • 6
    This solution works, yes, but only if a user is logged onto the server all the time. From http://support2.microsoft.com/kb/176799, reads: "The Interactive User: the application will run using the security context of the user currently logged onto the computer. If this option is selected and the user is not logged on, then the application will not start." – GR7 Sep 29 '14 at 13:28
  • 1
    Wow, you're a super star Gianni! Took me a week to find this post! – Yster Nov 19 '14 at 08:34
  • This [article](http://theether.net/download/Microsoft/kb/288367.html) provides help for automating with a specified user. – avrahamcool May 04 '15 at 11:19
  • This was a huge help. Thanks! – campbelt Jul 10 '15 at 18:06
  • 1
    I also had to add the user running my service to the list of Launch and Activation Permissions & Access Permissions on the Security tab. – Roman Starkov Mar 14 '16 at 13:42
  • @Gianni B. , I'm not able to find "Microsoft Word **** " – ShaileshDev Mar 30 '17 at 07:28
  • Just like @RomanStarkov I had to edit the Permissions. I also specified the user rather than using "interactive User" since this command is run by a dev ops pipeline so user wont always be logged in. Thanks so much!! – toujames Sep 13 '22 at 20:04
31

Try this; it may help you.

Create a new "Desktop" directory inside of

  • "C:\Windows\SysWOW64\config\systemprofile"

  • "C:\Windows\System32\config\systemprofile"

It works for me after a long, long day searching for the solution.

It seems to be a profile problem.

Nachiket
  • 511
  • 5
  • 13
6

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.

Kevin Cloet
  • 2,956
  • 1
  • 19
  • 36
  • We were having issue running Word as scheduled task where previously it required the user to be logged in. When reconfiguring the task to run under a new account it stopped working. This allowed the task to run without the user being logged in. – Alexander Higgins Aug 29 '22 at 19:59
2

To solve this I had to do both things:

  • Create the directories:

    • C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft \Windows\INetCac‌he
    • 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:

  1. Start Menu > regedit
  2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{00020906-0000-0000-C000-000000000046}
  3. Right click > New > String value

    • For Key, put RunAs
    • For Value, put Interactive User

You can do the same for Excel on AppID {00020812-0000-0000-C000-000000000046}.

vmassuchetto
  • 1,529
  • 1
  • 20
  • 44
1

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

MJSL
  • 51
  • 1
  • 7
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – OhBeWise Sep 29 '15 at 19:33
0

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.

Community
  • 1
  • 1
Killnine
  • 5,728
  • 8
  • 39
  • 66
0

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.

Ben
  • 398
  • 2
  • 8