Open and modify Word Document

93,785

Solution 1

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;

Solution 2

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");;

Solution 3

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");
Share:
93,785
ilamaiolo
Author by

ilamaiolo

Updated on July 05, 2022

Comments

  • ilamaiolo
    ilamaiolo almost 2 years

    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.

  • gap
    gap almost 10 years
    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 <e-iceblue.com/Introduce/word-for-net-introduce.html>
  • Hugh Seagraves
    Hugh Seagraves almost 7 years
    Can this actually open and read or does it just do conversions?