none
Cannot open Word document after publishing (Office.Interop.Word) RRS feed

  • Question

  • I cannot open Word document in c# after publishing to IIS

    It can open Word document and replace any text in debug mode by using Microsoft.Interop.Office.Word. When I publish the project, it return the following error:

    object reference not set to an instance of an object

    Here is my code:

    try {

    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };

    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true);

    doc.Activate();

    ..

    }

    The file path is correct. I found that the doc object returns null value.

    Working environment:

    - Windows Server 2012
    - Visual Studio Professional 2012
    - .Net framework 3.5
    - Microsoft Word 2007

    I try to add IUSR account and set enough permission to the folder which contains Word documents. It stills return the same error message. But it can work properly in debug mode...

    Thanks for your help


    Monday, February 17, 2014 7:11 AM

Answers

All replies

  • Hello Tony,

    First of all, I'd recommend breaking the chain of calls into single lines of code:

    wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true);

    Thus, you will be able to find the problematic line of code (property or method call). Then I would recommend specify the arguments explicitly passing the Type.Missing for optional parameters.

    What filename is passed to the Open method on the end user machine?

    Finally, I have noticed that you try to automate Word on the server machine. The Considerations for server-side Automation of Office article states the following for this:

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    Monday, February 17, 2014 3:19 PM
  • Hello Eugene,

    I try to publish the project to IIS with Microsoft Word 2010 (32 bit) now but it stills return following error message.

    Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005

    Filename format is: C:\WEB-DEV\TempFile\Letter\201402\CD-1.22-94ce4181d03e49c2ad02da5cd1f396b0.doc

    Anyway, it can work in debug mod. I don't think the problem is caused by filename

    Wednesday, February 19, 2014 11:31 AM
  • Hi tonylck

    I've moved your question to the more appropriate Word for Developers forum as you aren't using the VSTO technology.

    As Eugene mentions, what you want to do - running Office server-side - is not a supported scenario. It can be done, but I've never tried it. From what I've seen, though, I'm guessing it's a permissions problem and that you need to look into how to properly configure DCOM to allow you to open the file remotely.

    What is it you need to do with this document, server-side? Which version file format is it? Perhaps there's a supported way to achieve your task...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, February 19, 2014 2:43 PM
    Moderator
  • > Filename format is: C:\WEB-DEV\TempFile\Letter\201402\CD-1.22-94ce4181d03e49c2ad02da5cd1f396b0.doc

    Did you try to remove the dot symbol from the filename?

    Also I have found some similar forum threads for you:

    Accessing Office Word object model through asp.net results in “failed due to the following error: 80070005 Access is denied.”

    MS word component not working in asp.net application in my server

    Exception from HRESULT: 0x80070005 -Creating Word Document

    Wednesday, February 19, 2014 3:36 PM
  • Hello Eugene,

    I followed your provided links and solved the problem finally. 

    The problem is caused by permission of Word COM  


    Thanks,

    Tony

    Monday, February 24, 2014 3:40 PM
  • Good news, Tony!

    Thank you for sharing the solution for other forum readers.

    Monday, February 24, 2014 3:41 PM
  • hi Tony,

    I am experiencing the same issue in a C++ based service running on Windows 8 trying to open a document

    Can you please elaborate on how where the Word COM object is, and how you set the permission.

    thanks

    Ashwin

    Tuesday, August 12, 2014 6:58 PM