19
votes

I want to open a word file saved in my server using "Microsoft.Office.Interop.Word". This is my code:

    object missing = System.Reflection.Missing.Value;
    object readOnly = false;
    object isVisible = true;
    object fileName = "http://localhost:52099/modelloBusta/prova.dotx";
    Microsoft.Office.Interop.Word.ApplicationClass applicationWord = new Microsoft.Office.Interop.Word.ApplicationClass();
    Microsoft.Office.Interop.Word.Document modelloBusta = new  Microsoft.Office.Interop.Word.Document();

    try
    {

        modelloBusta = applicationWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref  missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,ref missing, ref missing, ref missing, ref missing);
        modelloBusta.Activate();



    }
    catch (COMException eccezione){
        Console.Write(eccezione);
        modelloBusta.Application.Quit(ref missing, ref missing, ref missing);

    }

In the windows task manager the process is present, but the "word document" doesn't appear (the application does not start). What is the problem? Thanks in advance.

3
Try OpenXML SDK docs ,and DownloadElyor
And for your problem for very good answer's : 1->Read *.dotx file from your Remote Url in the Stream field, 2-> OpenXMl Word Document in merge your loginc here...Elyor
Try to use docx type documents; that way you can treat them as xml. Also, you have to have a licensed and activated copy of Word on your server to use those functions.user220583
I solved with this command:applicationWord.Visibile = true;I hope you help someoneilamaiolo
You don't need the ref missing dummy parameters unless you have to specify special parameters such as read only, just use applicationWord.Documents.Open(fileName); Side note: Newer .NET versions are solving this with dynamic parameters, see here. So you can simply write applicationWord.Documents.Open(filePath, ReadOnly: true);Matt

3 Answers

22
votes

You need to make sure that the Word application window actually is made visible when automating Word like that:

var applicationWord = new Microsoft.Office.Interop.Word.Application();
applicationWord.Visible = true;
7
votes

first add the dll of office.interop by adding directly to the resources then add this using directive:

using Microsoft.Office.Interop.Word;

and use the following code

Application ap = new Application();
Document document = ap.Documents.Open(@"C:\invoice.docx");;
5
votes

http://support.microsoft.com/kb/257757

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.

http://freeword.codeplex.com/

Document document = new Document();
document.LoadFromFile("test.doct");