Open and modify Word Document

ilamaiolo picture ilamaiolo · Apr 27, 2013 · Viewed 90.7k times · Source

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.

Answer

Dirk Vollmar picture Dirk Vollmar · Apr 30, 2013

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;