19

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
ilamaiolo
  • 345
  • 2
  • 4
  • 14
  • 1
    Try [OpenXML SDK](http://msdn.microsoft.com/en-us/library/bb448854%28v=office.14%29.aspx) docs ,and [Download](http://www.microsoft.com/en-us/download/details.aspx?id=5124) – Elyor Apr 27 '13 at 14:52
  • 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](http://msdn.microsoft.com/en-us/library/office/ff478386.aspx) in merge your loginc here... – Elyor Apr 27 '13 at 14:54
  • 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. –  Apr 27 '13 at 15:42
  • 1
    I solved with this command:applicationWord.Visibile = true;I hope you help someone – ilamaiolo Apr 27 '13 at 17:06
  • 1
    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](http://stackoverflow.com/a/2690837/1016343). So you can simply write `applicationWord.Documents.Open(filePath, ReadOnly: true);` – Matt Nov 17 '15 at 16:39

3 Answers3

22

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;
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
7

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");;
afrischke
  • 3,836
  • 17
  • 30
sulman
  • 71
  • 1
  • 1
5

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");
PeterHapen
  • 51
  • 1
  • 1
  • 4
    Important to note that although this approach works, it has limitations: Spire.Doc free version is limited to 100 paragraphs. This limitation is enforced during reading or writing files. Upgrade to Commercial edition of Spire.Doc – gap Jul 31 '14 at 02:50
  • Can this actually open and read or does it just do conversions? – Hugh Seagraves May 18 '17 at 21:02