none
When closing a Word Document the Word UI appears RRS feed

  • Question

  • Hi there, I have a question regarding using Word inside a .NET ( C# ) application. Everytime I close a document the Word UI shows up, even though, I explicitly tell the Word Application object to not be visible. Here is the code that replicates my problem:

    using Word = Microsoft.Office.Interop.Word;
    
    Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    
    wordApp.Visible = false;
    
    Word.Document doc = wordApp.Documents.Open("c:\\test.docx");
    
    wordApp.Documents.Close();
    
    

    How can prevent the UI to appear?

     

    Thanks,

    Christian

     

    Thursday, March 31, 2011 2:48 PM

Answers

  • Hi Rich, thanks a lot. Instead of calling close for all documents I just close each document individually. This is fine for my use case and it works the way I want it to. Still I wonder why my original code produces the undesired behavior?

     

    Here is the code that works for me:

    Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.Visible = false;
    
    Word.Document doc = wordApp.Documents.Open("c:\\test.docx");
    doc.Close();
    
    

    Regards,

    Christian

    • Marked as answer by Bessie Zhao Friday, April 8, 2011 10:06 AM
    Thursday, March 31, 2011 6:59 PM

All replies

  • Hi Christian,

    Try this, it's written in VB so you'll have to convert.

    wordApp.Documents(doc).Close

    If wordApp.Documents.Count < 1 then wordApp.Quit

    Hope this helps


    Regards
    Thursday, March 31, 2011 3:10 PM
  • Hi Rich, thanks a lot. Instead of calling close for all documents I just close each document individually. This is fine for my use case and it works the way I want it to. Still I wonder why my original code produces the undesired behavior?

     

    Here is the code that works for me:

    Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.Visible = false;
    
    Word.Document doc = wordApp.Documents.Open("c:\\test.docx");
    doc.Close();
    
    

    Regards,

    Christian

    • Marked as answer by Bessie Zhao Friday, April 8, 2011 10:06 AM
    Thursday, March 31, 2011 6:59 PM
  • Hi Christian,

    I'm glad to hear it worked.

    I don't know why your original code gave the results it did but this other method has always worked for me so that's why I suggested it.

    Have a great day!


    Regards
    Thursday, March 31, 2011 7:13 PM