Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open and modify Word Document

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.

like image 204
ilamaiolo Avatar asked Apr 27 '13 14:04

ilamaiolo


3 Answers

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;
like image 193
Dirk Vollmar Avatar answered Oct 22 '22 06:10

Dirk Vollmar


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");;
like image 31
sulman Avatar answered Oct 22 '22 06:10

sulman


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");
like image 25
PeterHapen Avatar answered Oct 22 '22 05:10

PeterHapen